正如標題所言使用js實現按鈕控制文本框加1減1,此類主要應用於小時+分鐘,下面有個不錯的示例,喜歡的朋友可以參考下
time.html代碼: 代碼如下: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> <script type="text/javascript" src="jquery-1.8.2.min.js"></script> <script type="text/javascript" src="time.js"></script> </head> <body> <input type="button" value="小時加" onClick="add_hour();"> <input type="text" value="00" id="hour"> <input type="button" value="小時減" onClick="sub_hour();"> <br/> <br/> <input type="button" value="分鐘加" onClick="add_minute();"> <input type="text" value="00" id="minute"> <input type="button" value="分鐘減" onClick="sub_minute();"> </body> </html> time.js代碼: 代碼如下: function add_num(a,b,c){ if(c<a){ c++; } else{ c=b; } if(c<=9&&c>=0){ c="0"+c; } return c; } function sub_num(a,b,c){ if(c>a){ c--; } else{ c=b; } if(c<=9&&c>=0){ c="0"+c; } return c; } function add_hour(){ var current_num=$("#hour").attr("value"); current_num=add_num(23,0,current_num); $("#hour").attr("value",current_num); } function sub_hour(){ var current_num=$("#hour").attr("value"); current_num=sub_num(0,23,current_num); $("#hour").attr("value",current_num); } function add_minute(){ var current_num=$("#minute").attr("value"); current_num=add_num(59,0,current_num); $("#minute").attr("value",current_num); } function sub_minute(){ var current_num=$("#minute").attr("value"); current_num=sub_num(0,59,current_num); $("#minute").attr("value",current_num); }