Share.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. var Share=function(){}
  2. /**
  3. * 传入字符串,取得相应的拼音
  4. * @param conf Object {input:'用于计算拼音的字符串',preCallback:Function,postCallback:Function}),只有参数input是必填
  5. * @returns String 传入字符串对应的拼音
  6. */
  7. Share.getPingyin= function(conf)
  8. {
  9. var v=true;
  10. if(!conf){
  11. v=false;
  12. }else if(typeof(conf)=='object'){
  13. if(!conf.input){
  14. v=false;
  15. }
  16. } else {
  17. conf = {
  18. input : conf
  19. };
  20. }
  21. if(!v){
  22. throw new Error("方法GetPingyin用法:GetPingyin(String) 或 GetPingyin({input:'用于计算拼音的字符串',preCallback:Function,postCallback:Function}),只有参数input是必填!");
  23. }
  24. var py;
  25. var url=__ctx + "/platform/system/share/getPingyin.ht?input="+encodeURI(encodeURI(conf.input));
  26. url=url.getNewUrl();
  27. $.ajax(url, {
  28. async : false,
  29. beforeSend : function() {
  30. conf.preCallback && conf.preCallback();
  31. }
  32. }).done(function(data) {
  33. conf.postCallback && conf.postCallback(data);
  34. py=data.output;
  35. });
  36. return py;
  37. };
  38. /**
  39. * 绑定拼音转换。
  40. * source:需要转换的源对象。
  41. * target:目标对象。
  42. * 参数均为jquery对象。
  43. */
  44. Share.setPingyin=function(source,target){
  45. if(source.val().length==0) return ;
  46. if(target.val().length>0) return;
  47. Share.getPingyin({
  48. input:source.val(),
  49. postCallback:function(data){
  50. target.val(data.output);
  51. }
  52. });
  53. }
  54. /**
  55. * 绑定拼音转换。(大写)
  56. * source:需要转换的源对象。
  57. * target:目标对象。
  58. * 参数均为jquery对象。
  59. */
  60. Share.setPingyinUpperCase=function(source,target){
  61. if(source.val().length==0) return ;
  62. if(target.val().length>0) return;
  63. Share.getPingyin({
  64. input:source.val(),
  65. postCallback:function(data){
  66. target.val(data.output.toUpperCase());
  67. }
  68. });
  69. }