一种保存变量的方法
今天在研究司徒正美的伪事件系统的代码,跟他学到了一个保存变量的方法。之前我保存变量多数都是用 jquery 的 data 方法来保存。
现在才知道其实用 {} 或 [] 里的属性和方法用保存也是不错的法子。
1 function ctdClickEvent(e)
2 {
3 if( !-[1,] )
4 {
5 var readonly = "readOnly";
6 var obj = window.event.srcElement;
7 }else{
8 var readonly = "readonly";
9 var obj = e.currentTarget;
10 }
11
12 var id = obj.id.replace("btn_","");
13
14 if( obj.value=="此项不续费" )
15 {
16 var Renewal = {};
17 Renewal.startdate = $id("new_startdate2_" + id).value;
18 Renewal.enddate = $id("new_enddate2_" + id).value;
19 Renewal.price = $id("price2_" + id).value;
20 Renewal.transprice = $id("transprice2_" + id).value;
21
22 obj.Renewal = Renewal;
23
24 if( typeof($id("price2_" + id).value)=="number" )
25 $id("order_sum").value = parseInt($id("order_sum").value) - parseInt($id("price2_" + id).value);
26 if( typeof($id("transprice2_" + id).value)=="number" )
27 $id("trans_sum").value = parseInt($id("trans_sum").value) - parseInt($id("transprice2_" + id).value);
28
29 var startdate = $id("or_startdate2_" + id);
30 var enddate = $id("or_enddate2_" + id);
31 $id("new_startdate2_" + id).value = startdate.value;
32 $id("new_enddate2_" + id).value = enddate.value;
33 $id("price2_" + id).value = "0.00";
34 $id("transprice2_" + id).value = "0.00";
35 $id("guestneed2_" + id).value = "";
36 obj.value = "此项续费";
37
38 $id("new_startdate2_" + id).setAttribute(readonly,"readonly");
39 $id("new_enddate2_" + id).setAttribute(readonly,"readonly");
40 $id("price2_" + id).setAttribute(readonly,"readonly");
41 $id("transprice2_" + id).setAttribute(readonly,"readonly");
42 $id("guestneed2_" + id).setAttribute(readonly,"readonly");
43
44 $.datepicker._destroyDatepicker($id("new_startdate2_" + id));
45 $.datepicker._destroyDatepicker($id("new_enddate2_" + id));
46 }else{
47 $id("new_startdate2_" + id).value = obj.Renewal.startdate;
48 $id("new_enddate2_" + id).value = obj.Renewal.enddate;
49 $id("price2_" + id).value = obj.Renewal.price;
50 $id("transprice2_" + id).value = obj.Renewal.transprice;
51
52 if( typeof($id("price2_" + id).value)=="number" )
53 $id("order_sum").value = parseInt($id("order_sum").value) + parseInt($id("price2_" + id).value);
54 if( typeof($id("transprice2_" + id).value)=="number" )
55 $id("trans_sum").value = parseInt($id("trans_sum").value) + parseInt($id("transprice2_" + id).value);
56
57 obj.value = "此项不续费";
58
59 $id("new_startdate2_" + id).removeAttribute(readonly);
60 $id("new_enddate2_" + id).removeAttribute(readonly);
61 $id("price2_" + id).removeAttribute(readonly);
62 $id("transprice2_" + id).removeAttribute(readonly);
63 $id("guestneed2_" + id).removeAttribute(readonly);
64
65 $("#new_startdate2_" + id).datepicker();
66 $("#new_enddate2_" + id).datepicker();
67
68 }
69 }
2 {
3 if( !-[1,] )
4 {
5 var readonly = "readOnly";
6 var obj = window.event.srcElement;
7 }else{
8 var readonly = "readonly";
9 var obj = e.currentTarget;
10 }
11
12 var id = obj.id.replace("btn_","");
13
14 if( obj.value=="此项不续费" )
15 {
16 var Renewal = {};
17 Renewal.startdate = $id("new_startdate2_" + id).value;
18 Renewal.enddate = $id("new_enddate2_" + id).value;
19 Renewal.price = $id("price2_" + id).value;
20 Renewal.transprice = $id("transprice2_" + id).value;
21
22 obj.Renewal = Renewal;
23
24 if( typeof($id("price2_" + id).value)=="number" )
25 $id("order_sum").value = parseInt($id("order_sum").value) - parseInt($id("price2_" + id).value);
26 if( typeof($id("transprice2_" + id).value)=="number" )
27 $id("trans_sum").value = parseInt($id("trans_sum").value) - parseInt($id("transprice2_" + id).value);
28
29 var startdate = $id("or_startdate2_" + id);
30 var enddate = $id("or_enddate2_" + id);
31 $id("new_startdate2_" + id).value = startdate.value;
32 $id("new_enddate2_" + id).value = enddate.value;
33 $id("price2_" + id).value = "0.00";
34 $id("transprice2_" + id).value = "0.00";
35 $id("guestneed2_" + id).value = "";
36 obj.value = "此项续费";
37
38 $id("new_startdate2_" + id).setAttribute(readonly,"readonly");
39 $id("new_enddate2_" + id).setAttribute(readonly,"readonly");
40 $id("price2_" + id).setAttribute(readonly,"readonly");
41 $id("transprice2_" + id).setAttribute(readonly,"readonly");
42 $id("guestneed2_" + id).setAttribute(readonly,"readonly");
43
44 $.datepicker._destroyDatepicker($id("new_startdate2_" + id));
45 $.datepicker._destroyDatepicker($id("new_enddate2_" + id));
46 }else{
47 $id("new_startdate2_" + id).value = obj.Renewal.startdate;
48 $id("new_enddate2_" + id).value = obj.Renewal.enddate;
49 $id("price2_" + id).value = obj.Renewal.price;
50 $id("transprice2_" + id).value = obj.Renewal.transprice;
51
52 if( typeof($id("price2_" + id).value)=="number" )
53 $id("order_sum").value = parseInt($id("order_sum").value) + parseInt($id("price2_" + id).value);
54 if( typeof($id("transprice2_" + id).value)=="number" )
55 $id("trans_sum").value = parseInt($id("trans_sum").value) + parseInt($id("transprice2_" + id).value);
56
57 obj.value = "此项不续费";
58
59 $id("new_startdate2_" + id).removeAttribute(readonly);
60 $id("new_enddate2_" + id).removeAttribute(readonly);
61 $id("price2_" + id).removeAttribute(readonly);
62 $id("transprice2_" + id).removeAttribute(readonly);
63 $id("guestneed2_" + id).removeAttribute(readonly);
64
65 $("#new_startdate2_" + id).datepicker();
66 $("#new_enddate2_" + id).datepicker();
67
68 }
69 }