jquery.edit.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. $(document).ready(function(){
  2. var url = window.location.search;
  3. var params = url.substr(1).split('&');
  4. var act = '';
  5. var op = '';
  6. for(var j=0; j < params.length; j++)
  7. {
  8. var param = params[j];
  9. var arr = param.split('=');
  10. if(arr[0] == 'act')
  11. {
  12. act = arr[1];
  13. }
  14. if(arr[0] == 'op')
  15. {
  16. sort = arr[1];
  17. }
  18. }
  19. //给需要修改的位置添加修改行为
  20. $('span[nc_type="inline_edit"]').click(function(){
  21. var s_value = $(this).text();
  22. var s_name = $(this).attr('fieldname');
  23. var s_id = $(this).attr('fieldid');
  24. var req = $(this).attr('required');
  25. var type = $(this).attr('datatype');
  26. var max = $(this).attr('maxvalue');
  27. var ajax_branch = $(this).attr('ajax_branch');
  28. $('<input type="text">')
  29. .attr({value:s_value})
  30. .insertAfter($(this))
  31. .focus()
  32. .select()
  33. .keyup(function(event){
  34. if(event.keyCode == 13)
  35. {
  36. if(req)
  37. {
  38. if(!required($(this).attr('value'),s_value,$(this)))
  39. {
  40. return;
  41. }
  42. }
  43. if(type)
  44. {
  45. if(!check_type(type,$(this).attr('value'),s_value,$(this)))
  46. {
  47. return;
  48. }
  49. }
  50. if(max)
  51. {
  52. if(!check_max($(this).attr('value'),s_value,max,$(this)))
  53. {
  54. return;
  55. }
  56. }
  57. $(this).prev('span').show().text($(this).attr("value"));
  58. //branch ajax 分支
  59. //id 修改内容索引标识
  60. //column 修改字段名
  61. //value 修改内容
  62. $.get('index.php?act='+act+'&op=ajax',{branch:ajax_branch,id:s_id,column:s_name,value:$(this).attr('value')},function(data){
  63. if(data === 'false')
  64. {
  65. alert('名称已经存在,请您换一个');
  66. $('span[fieldname="'+s_name+'"][fieldid="'+s_id+'"]').text(s_value);
  67. return;
  68. }
  69. });
  70. $(this).remove();
  71. }
  72. })
  73. .blur(function(){
  74. if(req)
  75. {
  76. if(!required($(this).attr('value'),s_value,$(this)))
  77. {
  78. return;
  79. }
  80. }
  81. if(type)
  82. {
  83. if(!check_type(type,$(this).attr('value'),s_value,$(this)))
  84. {
  85. return;
  86. }
  87. }
  88. if(max)
  89. {
  90. if(!check_max($(this).attr('value'),s_value,max,$(this)))
  91. {
  92. return;
  93. }
  94. }
  95. $(this).prev('span').show().text($(this).attr('value'));
  96. $.get('index.php?act='+act+'&op=ajax',{branch:ajax_branch,id:s_id,column:s_name,value:$(this).attr('value')},function(data){
  97. if(data === 'false')
  98. {
  99. alert('名称已经存在,请您换一个');
  100. $('span[fieldname="'+s_name+'"][fieldid="'+s_id+'"]').text(s_value);
  101. return;
  102. }
  103. });
  104. $(this).remove();
  105. });
  106. $(this).hide();
  107. });
  108. $('span[nc_type="inline_edit_textarea"]').click(function(){
  109. var s_value = $(this).text();
  110. var s_name = $(this).attr('fieldname');
  111. var s_id = $(this).attr('fieldid');
  112. var req = $(this).attr('required');
  113. var type = $(this).attr('datatype');
  114. var max = $(this).attr('maxvalue');
  115. var ajax_branch = $(this).attr('ajax_branch_textarea');
  116. $('<textarea>')
  117. .attr({value:s_value})
  118. .appendTo($(this).parent())
  119. .focus()
  120. .select()
  121. .keyup(function(event){
  122. if(event.keyCode == 13)
  123. {
  124. if(req)
  125. {
  126. if(!required($(this).attr('value'),s_value,$(this)))
  127. {
  128. return;
  129. }
  130. }
  131. if(type)
  132. {
  133. if(!check_type(type,$(this).attr('value'),s_value,$(this)))
  134. {
  135. return;
  136. }
  137. }
  138. if(max)
  139. {
  140. if(!check_max($(this).attr('value'),s_value,max,$(this)))
  141. {
  142. return;
  143. }
  144. }
  145. $(this).prev('span').show().text($(this).attr("value"));
  146. //branch ajax 分支
  147. //id 修改内容索引标识
  148. //column 修改字段名
  149. //value 修改内容
  150. $.get('index.php?act='+act+'&op=ajax',{branch:ajax_branch,id:s_id,column:s_name,value:$(this).attr('value')},function(data){
  151. if(data === 'false')
  152. {
  153. alert('名称已经存在,请您换一个');
  154. $('span[fieldname="'+s_name+'"][fieldid="'+s_id+'"]').text(s_value);
  155. return;
  156. }
  157. });
  158. $(this).remove();
  159. }
  160. })
  161. .blur(function(){
  162. if(req)
  163. {
  164. if(!required($(this).attr('value'),s_value,$(this)))
  165. {
  166. return;
  167. }
  168. }
  169. if(type)
  170. {
  171. if(!check_type(type,$(this).attr('value'),s_value,$(this)))
  172. {
  173. return;
  174. }
  175. }
  176. if(max)
  177. {
  178. if(!check_max($(this).attr('value'),s_value,max,$(this)))
  179. {
  180. return;
  181. }
  182. }
  183. $(this).prev('span').show().text($(this).attr('value'));
  184. $.get('index.php?act='+act+'&op=ajax',{branch:ajax_branch,id:s_id,column:s_name,value:$(this).attr('value')},function(data){
  185. if(data === 'false')
  186. {
  187. alert('名称已经存在,请您换一个');
  188. $('span[fieldname="'+s_name+'"][fieldid="'+s_id+'"]').text(s_value);
  189. return;
  190. }
  191. });
  192. $(this).remove();
  193. });
  194. $(this).hide();
  195. });
  196. //给需要修改的图片添加异步修改行为
  197. $('img[nc_type="inline_edit"]').click(function(){
  198. var i_id = $(this).attr('fieldid');
  199. var i_name = $(this).attr('fieldname');
  200. var i_src = $(this).attr('src');
  201. var i_val = ($(this).attr('fieldvalue'))== 0 ? 1 : 0;
  202. var ajax_branch = $(this).attr('ajax_branch');
  203. $.get('index.php?act='+act+'&op=ajax',{branch:ajax_branch,id:i_id,column:i_name,value:i_val},function(data){
  204. if(data == 'true')
  205. {
  206. if(i_val == 0)
  207. {
  208. $('img[fieldid="'+i_id+'"][fieldname="'+i_name+'"]').attr({'src':i_src.replace('enabled','disabled'),'fieldvalue':i_val});
  209. }
  210. else
  211. {
  212. $('img[fieldid="'+i_id+'"][fieldname="'+i_name+'"]').attr({'src':i_src.replace('disabled','enabled'),'fieldvalue':i_val});
  213. }
  214. }
  215. });
  216. });
  217. $('a[nc_type="inline_edit"]').click(function(){
  218. var i_id = $(this).attr('fieldid');
  219. var i_name = $(this).attr('fieldname');
  220. var i_src = $(this).attr('src');
  221. var i_val = ($(this).attr('fieldvalue'))== 0 ? 1 : 0;
  222. var ajax_branch = $(this).attr('ajax_branch');
  223. $.get('index.php?act='+act+'&op=ajax',{branch:ajax_branch,id:i_id,column:i_name,value:i_val},function(data){
  224. if(data == 'true')
  225. {
  226. if(i_val == 0){
  227. $('a[fieldid="'+i_id+'"][fieldname="'+i_name+'"]').attr({'class':('enabled','disabled'),'title':('开启','关闭'),'fieldvalue':i_val});
  228. }else{
  229. $('a[fieldid="'+i_id+'"][fieldname="'+i_name+'"]').attr({'class':('disabled','enabled'),'title':('关闭','开启'),'fieldvalue':i_val});
  230. }
  231. }else{
  232. alert('响应失败');
  233. }
  234. });
  235. });
  236. //给每个可编辑的小图片的父元素添加可编辑标题 $('img[nc_type="inline_edit"]').parent().attr('title','可编辑');
  237. //给列表有排序行为的列添加鼠标手型效果
  238. $('span[nc_type="order_by"]').hover(function(){$(this).css({cursor:'pointer'});},function(){});
  239. });
  240. //检查提交内容的必须项
  241. function required(str,s_value,jqobj)
  242. {
  243. if(str == '')
  244. {
  245. jqobj.prev('span').show().text(s_value);
  246. jqobj.remove();
  247. alert('此项不能为空');
  248. return 0;
  249. }
  250. return 1;
  251. }
  252. //检查提交内容的类型是否合法
  253. function check_type(type, value, s_value, jqobj)
  254. {
  255. if(type == 'number')
  256. {
  257. if(isNaN(value))
  258. {
  259. jqobj.prev('span').show().text(s_value);
  260. jqobj.remove();
  261. alert('此项仅能为数字');
  262. return 0;
  263. }
  264. }
  265. if(type == 'int')
  266. {
  267. var regu = /^-{0,1}[0-9]{1,}$/;
  268. if(!regu.test(value))
  269. {
  270. jqobj.prev('span').show().text(s_value);
  271. jqobj.remove();
  272. alert('此项仅能为整数');
  273. return 0;
  274. }
  275. }
  276. if(type == 'pint')
  277. {
  278. var regu = /^[0-9]+$/;
  279. if(!regu.test(value))
  280. {
  281. jqobj.prev('span').show().text(s_value);
  282. jqobj.remove();
  283. alert('此项仅能为正整数');
  284. return 0;
  285. }
  286. }
  287. if(type == 'zint')
  288. {
  289. var regu = /^[1-9]\d*$/;
  290. if(!regu.test(value))
  291. {
  292. jqobj.prev('span').show().text(s_value);
  293. jqobj.remove();
  294. alert('此项仅能为正整数');
  295. return 0;
  296. }
  297. }
  298. if(type == 'discount')
  299. {
  300. var regu = /[1-9]|0\.[1-9]|[1-9]\.[0-9]/;
  301. if(!regu.test(value))
  302. {
  303. jqobj.prev('span').show().text(s_value);
  304. jqobj.remove();
  305. alert('只能是0.1-9.9之间的数字');
  306. return 0;
  307. }
  308. }
  309. return 1;
  310. }
  311. //检查所填项的最大值
  312. function check_max(str,s_value,max,jqobj)
  313. {
  314. if(parseInt(str) > parseInt(max))
  315. {
  316. jqobj.prev('span').show().text(s_value);
  317. jqobj.remove();
  318. alert('此项应小于等于'+max);
  319. return 0;
  320. }
  321. return 1;
  322. }
  323. //新的inline_edit调用方法
  324. //javacript
  325. //$('span[nc_type="class_sort"]').inline_edit({act: 'microshop',op: 'update_class_sort'});
  326. //html
  327. //<span nc_type="class_sort" column_id="<?php echo $val['class_id'];?>" title="<?php echo $lang['nc_editable'];?>" class="editable tooltip"><?php echo $val['class_sort'];?></span>
  328. //php
  329. //$result = array();
  330. //$result['result'] = FALSE;/TURE
  331. //$result['message'] = '错误';
  332. //echo json_encode($result);
  333. (function($) {
  334. $.fn.inline_edit= function(options) {
  335. var settings = $.extend({}, {open: false}, options);
  336. return this.each(function() {
  337. $(this).click(onClick);
  338. });
  339. function onClick() {
  340. var span = $(this);
  341. var old_value = $(this).html();
  342. var column_id = $(this).attr("column_id");
  343. $('<input type="text">')
  344. .insertAfter($(this))
  345. .focus()
  346. .select()
  347. .val(old_value)
  348. .blur(function(){
  349. var new_value = $(this).attr("value");
  350. if(new_value != '') {
  351. $.get('index.php?act='+settings.act+'&op='+settings.op+'&branch=ajax',{id:column_id,value:new_value},function(data){
  352. data = $.parseJSON(data);
  353. if(data.result) {
  354. span.show().text(new_value);
  355. } else {
  356. span.show().text(old_value);
  357. alert(data.message);
  358. }
  359. });
  360. } else {
  361. span.show().text(old_value);
  362. }
  363. $(this).remove();
  364. })
  365. $(this).hide();
  366. }
  367. }
  368. })(jQuery);
  369. (function($) {
  370. $.fn.inline_edit_confirm = function(options) {
  371. var settings = $.extend({}, {open: false}, options);
  372. return this.each(function() {
  373. $(this).click(onClick);
  374. });
  375. function onClick() {
  376. var $span = $(this);
  377. var old_value = $(this).text();
  378. var column_id = $(this).attr("column_id");
  379. var $input = $('<input type="text">');
  380. var $btn_submit = $('<a class="inline-edit-submit" href="JavaScript:;">确认</a>');
  381. var $btn_cancel = $('<a class="inline-edit-cancel" href="JavaScript:;">取消</a>');
  382. $input.insertAfter($span).focus().select().val(old_value);
  383. $btn_submit.insertAfter($input);
  384. $btn_cancel.insertAfter($btn_submit);
  385. $span.hide();
  386. $btn_submit.click(function(){
  387. var new_value = $input.attr("value");
  388. if(new_value !== '' && new_value !== old_value) {
  389. $.post('index.php?act=' + settings.act + '&op=' + settings.op, {id:column_id, value:new_value}, function(data) {
  390. data = $.parseJSON(data);
  391. if(data.result) {
  392. $span.text(new_value);
  393. } else {
  394. alert(data.message);
  395. }
  396. });
  397. }
  398. show();
  399. });
  400. $btn_cancel.click(function() {
  401. show();
  402. });
  403. function show() {
  404. $span.show();
  405. $input.remove();
  406. $btn_submit.remove();
  407. $btn_cancel.remove();
  408. }
  409. }
  410. };
  411. })(jQuery);