edit.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //检查输入
  2. var tip_top =50;
  3. //获取tip
  4. function getValDiv(exId) {
  5. var divObj;
  6. divObj = document.createElement("div");
  7. divObj.id = "val_div_" + exId;
  8. divObj.className = "val_div";
  9. return divObj;
  10. }
  11. function getAbsolutePosition(element) {
  12. var point = { x: element.offsetLeft, y: element.offsetTop };
  13. if (element.offsetParent) {
  14. var parentPoint = this.getAbsolutePosition(element.offsetParent);
  15. point.x += parentPoint.x;
  16. point.y += parentPoint.y;
  17. }
  18. return point;
  19. };
  20. //加载Tip
  21. function addTip(inputObj) {
  22. var inputWidth = parseInt($(inputObj).width());
  23. var exId = inputObj.id;
  24. var divObj = document.getElementById("val_div_" + exId);
  25. if (!divObj) {
  26. var point = getAbsolutePosition(inputObj);
  27. divObj = getValDiv(exId);
  28. divObj.style.top = (point.y - 52 +(tip_top)) + "px";
  29. divObj.style.left = (point.x + 140 + 20) + "px";
  30. /*
  31. divObj.style.top =(point.y+30) + "px";
  32. divObj.style.left = point.x + "px"; */
  33. var pObj = inputObj.parentNode;
  34. if (pObj.nodeName != "TD") pObj = pObj.parentNode;
  35. pObj.appendChild(divObj);
  36. }
  37. }
  38. //获取列
  39. function getCellIndex(o) {
  40. var obj = o.parentNode.cells;
  41. for (var i = 0; i < obj.length; i++) {
  42. if (o == obj[i]) return i;
  43. }
  44. }
  45. //显示错误
  46. function tipShowError(exId, errMsg) {
  47. if (errMsg) {
  48. $("#val_div_" + exId).empty();
  49. var str = "<div class=\"val_err_info\" id=\"val_err_info_" + exId + "\">";
  50. str += "<span id=\"val_err_" + exId + "\" class=\"val_err\" title=\"错误\"></span>";
  51. str += errMsg + "</div>";
  52. $("#val_div_" + exId).append($(str));
  53. $("#" + exId).bind({
  54. focus: function () {
  55. if ($("#val_div_" + exId).length > 0)
  56. $("#val_div_" + exId).empty();
  57. }
  58. });
  59. }
  60. }
  61. //显示正确
  62. function tipShowOk(exId) {
  63. //$("#" + exId).attr("class", "webtext");
  64. var str = "<span id=\"val_ok_" + exId + "\" class=\"val_ok\" title=\"正确\"></span>";
  65. $("#val_div_" + exId).append($(str));
  66. $("#" + exId).bind({
  67. focus: function () {
  68. if ($("#val_div_" + exId).length > 0)
  69. $("#val_div_" + exId).empty();
  70. }
  71. });
  72. }
  73. //计算字符数,一个中文2个字符
  74. function fLen(obj) {
  75. var nCNLenth = 0;
  76. var nLenth = obj.length;
  77. for (var i = 0; i < nLenth; i++) {
  78. if (obj.charCodeAt(i) > 255) {
  79. nCNLenth += 2;
  80. } else {
  81. nCNLenth++;
  82. }
  83. }
  84. return nCNLenth;
  85. }
  86. //运行函数
  87. function runFunction(sFunc) {
  88. eval(sFunc + "()");
  89. }
  90. //获取结果显示
  91. function getCheckMsg(sId, eMsg) {
  92. if (eMsg.length > 0) {
  93. tipShowError(sId, eMsg);
  94. return false;
  95. } else {
  96. tipShowOk(sId);
  97. return true;
  98. }
  99. }
  100. function checkInput(sId, msg, reg) {
  101. addTip($("#" + sId)[0]);
  102. var errMsg = "";
  103. var v = $("#" + sId).val();
  104. if (v == "") errMsg = msg + "不允许为空!";
  105. if (v == $("#" + sId).attr('def')) errMsg = msg + "不允许为空!";
  106. else if (reg && !new RegExp(reg).test(v)) {
  107. errMsg = msg + "不符合规范!";
  108. }
  109. return getCheckMsg(sId, errMsg);
  110. }
  111. function checkDefInput(sId, msg) {
  112. addTip($("#" + sId)[0]);
  113. var errMsg = "";
  114. if ($("#" + sId).val() == "" || $("#" + sId).val() == $("#" + sId).attr("def")) {
  115. errMsg = msg + "不允许为空!";
  116. $("#" + sId).val("");
  117. }
  118. return getCheckMsg(sId, errMsg);
  119. }
  120. //检查数字输入
  121. function checkNumInput(sId, msg, min) {
  122. addTip($("#" + sId)[0]);
  123. var errMsg = "";
  124. var v = $("#" + sId).val();
  125. if (v == "")
  126. errMsg = msg + "不允许为空!";
  127. else if (parseInt(v) < min)
  128. errMsg = msg + "不允许小于" + min;
  129. return getCheckMsg(sId, errMsg);
  130. }
  131. //检查日期
  132. function checkDateInput(sId, eId, msg) {
  133. addTip($("#" + sId)[0]);
  134. var errMsg = "";
  135. if ($("#" + sId).val() == "")
  136. return getCheckMsg(sId, msg + "不允许为空!");
  137. else if ($("#" + eId).val() == "")
  138. return getCheckMsg(eId, msg + "不允许为空!");
  139. return true;
  140. }
  141. function toWeight(money) {
  142. if (money == "") return 0;
  143. return Math.round(parseFloat(money) * 1000) / 1000;
  144. }
  145. function validCombobox(e) {
  146. var items = this.findItems(e.value);
  147. if (!items || items.length == 0) {
  148. e.sender.setValue("");
  149. }
  150. }
  151. $(document).ready(function () {
  152. if (/msie [1-7].0/.test(navigator.userAgent.toLowerCase()) && $("html")[0].scrollHeight > $("html").height()) {
  153. $("html").css("overflowY", "scroll");
  154. }
  155. });
  156. //对象,小数点,允许负数
  157. function allowNumber(obj, decimals, min, max, allowMinus) {
  158. var v = obj.value;
  159. if (!min) min = 0;
  160. //if (v == "") { obj.value = min; return; }
  161. if (!max) max = 10000;
  162. //默认不允许小数
  163. if (!allowMinus) allowMinus = false;
  164. //如果没有指定小数,则不允许小数
  165. if (!decimals) decimals = 0;
  166. var res = "";
  167. for (i = 0; i < v.length; i++) {
  168. if (i == 0 && v.charAt(0) == "-" && allowMinus == true)
  169. res = "-";
  170. else {
  171. //判断是否是数字
  172. if (/\d/.test(v.charAt(i)))
  173. res += v.charAt(i);
  174. else {
  175. if (v.charAt(i) == "." && decimals > 0) {
  176. res += v.charAt(i);
  177. }
  178. }
  179. }
  180. }
  181. if (res.length > 0) {
  182. //不允许0开头后面还有0
  183. if (res.substring(0, 1) == "0" && res.length > 1 && res.substring(0, 2) != "0.") {
  184. res = res.substring(1);
  185. }
  186. //负数不允许0开头后面还有0
  187. if (res.substring(0, 2) == "-0" && res.length > 2 && res.substring(0, 3) != "-0.") {
  188. res = res.substring(0, 2);
  189. }
  190. //有小数点
  191. if (res.indexOf(".") != -1) {
  192. //第一个字符不允许是小数点
  193. if (res.substring(0, 1) == ".") {
  194. res = res.replace(".", "");
  195. }
  196. else {
  197. //只允许有一个小数点
  198. if (res.indexOf(".") != res.lastIndexOf(".")) {
  199. res = res.substring(0, res.indexOf(".") + 1) + res.substring(res.indexOf(".") + 1).replace(".", "");
  200. }
  201. //不允许超出小数位
  202. if (res.substring(res.indexOf(".") + 1).length > decimals) {
  203. res = res.substring(0, res.indexOf(".") + decimals + 1);
  204. }
  205. }
  206. }
  207. if (parseFloat(res) < min) res = min;
  208. if (max > 0) {
  209. if (parseFloat(res) > max) res = max;
  210. }
  211. }
  212. obj.value = res;
  213. }
  214. //纠正数字
  215. function correctNumber(obj) {
  216. var v = obj.value;
  217. //删除只有一个负数符号的情况
  218. if (v == "-") v = "";
  219. //删除最后一个是小数点的情况
  220. var dIdx = v.indexOf(".");
  221. if (v.length == dIdx + 1)
  222. v = v.substring(0, dIdx);
  223. //if (v == "") v = 0;
  224. obj.value = v;
  225. }
  226. function upNumber(dObj, max) {
  227. var tr = dObj.parentNode.parentNode;
  228. var obj = tr.getElementsByTagName("input")[0];
  229. var sv = obj.value != "" ? parseFloat(obj.value) : 0;
  230. if (sv + 1 <= max) {
  231. obj.value = sv + 1;
  232. }
  233. }
  234. function downNumber(dObj, min) {
  235. var tr = dObj.parentNode.parentNode;
  236. var obj = tr.getElementsByTagName("input")[0];
  237. var sv = obj.value != "" ? obj.value : 0;
  238. if (sv - 1 >= min) {
  239. obj.value = sv - 1;
  240. }
  241. }
  242. function closeFn(flag) {
  243. if (window.CloseOwnerWindow) {
  244. if (flag != undefined) window.CloseOwnerWindow("save");
  245. else window.CloseOwnerWindow("close");
  246. }
  247. else {
  248. window.close();
  249. }
  250. }
  251. $(document).keydown(function (e) {
  252. if (e.which == 8) {
  253. var targ = null;
  254. if (e.target) targ = e.target;
  255. else targ = e.srcElement;
  256. if ((targ.type == "textarea" && (targ.getAttribute("readonly") == null || targ.getAttribute("readonly") == "")) || targ.type == "text") return true;
  257. else return false;
  258. } else if (e.which == 13) {
  259. hideKB();
  260. closeKB();
  261. }
  262. });
  263. function closeKB() {
  264. }