日期:2014-05-16  浏览次数:20344 次

js实现按钮加减,代码有点问题..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script>
  function add(index){
    document.getElementsById(index+'num').value = document.getElementsById(index+'num').value*1+1;
    
  }
    function min(index){
    document.getElementsById(index+'num').value = document.getElementsById(index+'num').value*1-1;
  }
  
  </script>
 </HEAD>
 
 <BODY>
  <p> <input type="text" id='1num' name="T1" size="20" value="0"> 
  <input type="button" id='1' width=50px value="+" onclick="add(1)">
  <input type="button" id='1' width=50px value="-" onclick="min(1)"></p> 
  <p> <input type="text" id='2num' name="T1" size="20" value="0"> 
<input type="button" id='2' width=50px value="+" onclick="add(2)">
  <input type="button" id='2' width=50px value="-" onclick="min(2)"></p> 
  <p> <input type="text" id='3num' name="T1" size="20" value="0">
<input type="button" id='3' width=50px value="+" onclick="add(3)">
  <input type="button" id='3' width=50px value="-" onclick="min(3)"></p> 
 
 </BODY>
</HTML>



运行之后点+ -都没有反应,不知道是哪边有问题? 谢谢!

------解决方案--------------------
  function add(index){
    document.getElementById(index+'num').value = document.getElementById(index+'num').value*1+1;
     
  }
    function minx(index){
    document.getElementById(index+'num').value = document.getElementById(index+'num').value*1-1;
  }
是document.getElementById  因为id应该是唯一的  所以得到的对象也是一个 所以没有s

其次减的时候不要定义为min 貌似是个关键字的样子