2010-04-30 81 views
0

我有一個組合框(ASP.NET下拉列表控件)和文本框(使用DIV ID txtName)的小型窗體。當組合框的選定索引更改時,我想清除文本框。jquery asp.net組合框問題

據我所知,後續清除文本框的值:

$("#txtName").val(''); 

的東西是組合框。它包含一個表示一年中幾個月的整數列表。下拉控件稱爲ddlMonths。

$("#ddlMonths").change(function() { 
    $("#txtName").val(''); 
}); 

我想利用變化,一個onSelectedIndexChange事件處理程序將與此控件關聯。

我也試過(因爲我跑進ASP.NET被錯位瓦特/ jQuery的客戶端ID)這樣的:

$("#<%=ddlMonths.ClientID%>").change(function() { 
    $("#<%=txtName.ClientID%>").val(''); 
}); 

既不做法似乎是工作。我錯過了什麼嗎?

回答

1

我剛剛意識到我的問題是什麼!

上述代碼是一個JavaScript客戶端塊,但是,我沒有:

$(document).ready(function() { 

// I put my code in here and then it worked. My problem was more than likely that 
// my code executed *before* the controls were rendered, and I need to have the code 
// ready to execute *after* the document completely rendered. 

}); 
+0

你的猜測是正確的。您也可以在表單之後移動「