2012-02-13 102 views
0

您好,感谢您的提前帮助。请注意,我对此很陌生,这可能会解释为什么我无法弄清楚这个问题。根据其他输入更改输入字段

我试图动态更改输入文本字段的值,当下拉列表的值发生变化时。我的网页

头:

<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
    <link rel="stylesheet" type="text/css" href="mystyle.css" /> 
    <script type="text/javascript" src="projectJS.js"></script> 
    <script type="text/javascript" src="jquery.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      // initialize the value of the input field to the selected value 
      // of the dropdown list: 
      $("#membfee").val($("#role").val()); 

      $("#role").click(function() { 
       // when the value of the dropdown list changes 
       // update the input field 
       $("#membfee").val("111"); 
      }); 
     });​ 
    </script>   
</head> 

我不事没有与jQuery的任何问题,这一切都非常简单。你能像上面那样引用外部JavaScript文件和jQuery吗?

节包含下拉菜单和文本字段我想更新的身体:

<tr> 
    <td class="label_block">Membership Term:</td> 
    <td> <select name="role" id="role" /> 
       <option value="Trial">Trial</option> 
       <option value="1 Month">1 Month</option> 
       <option value="3 Month">3 Month</option> 
       <option value="6 Month">6 Month</option> 
       <option value="Year">Year</option> 
      </select> 
    </td> 
</tr> 
<tr> 
    <td class ="label_block">Membership Fee:</td> 
    <td><input type="text" size="15" name="membfee" id="membfee" value="" /></td> 
    <td class="error"> 
     <%out.print(membfee_msg);%> 
    </td> 
</tr> 

回答

2

你应该使用.change()

$("#role").change(function() { 
    // when the value of the dropdown list changes 
    // update the input field 
    $("#membfee").val("111"); 
}); 

你肯定不会有任何JS错误该页面,因为它just should work

+0

对不起,我其实是有它作为.change()函数,并刚刚在刚刚点击的方法来试试吧。 – user1207869 2012-02-13 22:33:04

+0

它应该工作。也许你在页面上有错误?什么是ProtectJS?也许它与jQuery冲突。 – PeeHaa 2012-02-13 22:34:10

+0

projectjs.js只有几个函数,我担心也许我不能同时引用这两个函数,但是当我注释掉链接到JavaScript文件的行时,问题仍然存在。这是一个JSP文件,可能是问题所在。 – user1207869 2012-02-13 22:41:11

1

尝试使用change()事件,而不是:

$("#role").change(function(){ 
    // blabla 
})