Keine Beschreibung

text.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. $.use(["jQuery", "form", "util"], function($, form, util) {
  2. var m_rd="不可为空或空字符串",trim = "trim",
  3. readOnly = "readOnly",
  4. showOnly = "showOnly",
  5. modelName = 'text',
  6. tInt = "int",
  7. tBool = "bool",
  8. tFloat = "float",
  9. tString = "string",
  10. pw = "password",
  11. def = "defVal",
  12. placeholder = "placeholder",
  13. required = "required";
  14. form.register(function($e) {
  15. var cls = util.classCheck($e[0], [modelName, trim, readOnly, showOnly, tInt, tBool, tFloat, pw,required ]);
  16. if(cls[modelName]) {
  17. var n = $e.attr("name") || $e.attr("id"),
  18. ve, rules = [];
  19. if(!n) {
  20. throw "Attribute[name] is invalid";
  21. }
  22. dv = $e.attr(def) || "";
  23. $e.empty();
  24. if(cls[showOnly]) {
  25. $e.text(dv);
  26. } else {
  27. ve = $("<input type='" + (cls[pw] ? pw : modelName) + "' />");
  28. var tmp = $e.attr(placeholder);
  29. if(tmp) ve.attr(placeholder, tmp);
  30. if(cls[readOnly]) ve.prop('readonly', "readonly");
  31. ve.val(dv);
  32. ve.appendTo($e);
  33. }
  34. return {
  35. name: n,
  36. get: function() {
  37. if(!cls[showOnly]) {
  38. var vd = cls[trim]?ve.val().trim():ve.val();
  39. if(vd) {
  40. if(cls[tInt]) {
  41. vd = parseInt(vd);
  42. if(!isNaN(vd)) { return vd };
  43. } else if(cls[tFloat]) {
  44. vd = parseFloat(vd);
  45. if(!isNaN(vd)) { return vd };
  46. } else {
  47. return vd;
  48. }
  49. }
  50. }
  51. },
  52. set: function(data) {
  53. if(cls[showOnly]) {
  54. $e.text(data)
  55. } else {
  56. ve.val(data);
  57. }
  58. },
  59. validate: function() {
  60. if(cls[required]) {
  61. if(!ve.val().trim()) {
  62. this.invalid(m_rd)
  63. return m_rd;
  64. }
  65. }
  66. return util.validate(rules, this);
  67. },
  68. addRules: function(rule) {
  69. util.addRules(rules, rule);
  70. },
  71. reset: function() {
  72. this.set(dv);
  73. },
  74. valid: function() { util.valid($e); },
  75. invalid: function(reson) {
  76. util.invalid($e);
  77. util.error(reson);
  78. }
  79. };
  80. }
  81. });
  82. });