2010-04-30 78 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

你的猜测是正确的。您也可以在表单之后移动“