2015-02-10 127 views
-1

我有麻烦写两个文本框事件,并希望一些帮助一点点......两个文本框活动的Javascript

我有以下几点:

<body> 
    <form action="#"> 
     <input type="text" id="begin" /> 
     <select id "toLeftBox"> 
     <option value = "a">Apple</option> 
     <option value = "b">Blueberry</option> 
     <option value = "c">Cherry</option> 
     </select> 
     = 
     <input type="text" id="done" /> 
     <select id= "toRightBox"> 
     <option value="a">Apple</option> 
     <option value="b">Blueberry</option> 
     <option value="c">Cherry</option> 
     </select> 
     <p id = "leftWeight"></p> 
     <p id = "rightWeight"></p> 
    </form> 
    <script> 
     var lftchoice = document.getElementById('begin'), 
      rgtchoice = document.getElementById('done'); 
      newLeft = document.getElementById("toLeftBox"); 
      newRight = document.getElementById("toRightBox"); 

     newValRight = function(){ 
     var tempR = document.getElementById("toRightBox").value; 
      if(tempR == "a"){ 
      rgtchoice.value = lftchoice.value * 1; 
      }else if(tempR == "b"){ 
      rgtchoice.value = lftchoice.value * 2; 
      }else if(tempR == "c"){ 
      rgtchoice.value = lftchoice.value * 3; 
      } 
     } 
     newValLeft = function(){ 
     var tempL = document.getElementById("toLeftBox").value; 
     if(tempL == tempR){ 
      lftchoice.value = rgtchoice.value*1; 
     }else if(tempL == "b"){ 
      lftchoice.value = rghtchoice.value * 2; 
     }else if(tempL == "c"){ 
      lftchoice.value = rgtchoice.value * 3; 
     } 
     } 
     lftchoice.onkeyup = newValRight; 
     rgtchoice.onkeyup = newValLeft; 
     newLeft.onchange = newValLeft; 
     newRight.onchange = newValRight; 
    </script> 
</body> 

我有两个文本框。左边的文本框('开始')将接受'选择的项目'的权重。但是正确的方框('done')应该根据用户从下拉文本列表中选择的内容更改数值。 将值输入到左侧框中时,右侧框更新正确,同时更改下拉列表中的项目。 但是,我希望它能够双向工作。 例如:

Left 23 [Apple] = Right 23 [Apple] 
Left 23 [Apple] = Right 46 [Blueberry] (if I change to blueberry on right list) 
Left 46 [Blueberry] = Right 46 [Blueberry] (if I keep the right, and just change lft list) 

简单地说,我想这两个文本框更新到正确的值相对于在对面框中选择水果/等价。

谢谢

+0

是newValue还是toRightBox?