2012-02-13 95 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 
})