我需要一个用于ASP.NET项目的组合框,所以我决定使用Ajax (http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ComboBox/ComboBox.aspx)。
我不想使用回发,因为我不想重新加载页面,但是我需要知道文本框中的文本何时被更改,这样我就可以调用服务器来持久化新的列表项。
我很好奇如何将onchange或onblur事件绑定到这个组合框使用的输入框。
这是我的asp.net页面:
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<cc1:ComboBox ID="PlantDropDown" runat="server" OnInit="PlantDropDown_Init" DropDownStyle="DropDown"
AutoCompleteMode="SuggestAppend"
ItemInsertLocation="OrdinalText" AutoPostBack="false">
</cc1:ComboBox>更新:我尝试使用这个建议,并得到了以下错误:
$find("PlantDropDown")为空
中断此错误
$find('PlantDropDown').add_propertyChanged(function(sender,e) {r\n
我在Javascript方面使用jQuery,顺便说一句,以防有帮助。
最后更新:
多亏了crescentfresh的帮助,我才使它工作起来,最后,我在我的.aspx文件中有了如下内容:
<input type="hidden" id="PlantDropDownID" value="<%= PlantDropDown.ClientID %>" />这是在我的javascript文件中,因为我不把javascript推到我的.aspx文件中:
elem = document.getElementById('PlantDropDownID');
$find(elem.value).add_propertyChanged(function(sender, e) {
if (e.get_propertyName() == 'selectedIndex') {
var newValue = sender.get_textBoxControl().value;
}
})发布于 2009-09-22 02:48:00
我相信您应该绑定到"propertyChanged"事件并检查对"selectedIndex"属性的更改:
$find('PlantDropDown').add_propertyChanged(function(sender, e) {
if (e.get_propertyName() == 'selectedIndex') {
var newValue = sender.get_textBoxControl().value;
// persist selected value here...
}
})通常关于.NET control ids in the client的警告。
api并不容易,这是肯定的。例如,没有.get_value()方法,这将是一个很好的方法,而不必通过嵌入的textbox控件。
编辑
$find("PlantDropDown")为空
确保您使用的是正确的id。见.NET control ids in the client。要获得引用,您可能需要:
$find('<%= PlantDropDown.ClientID %>')我使用jQuery作为javascript端
那是没有意义的。
发布于 2009-11-11 08:41:52
我发现,除非我将其包装成如下所示的函数,否则我无法获得所提供的答案。不知道这是为什么,但希望它能给别人一些痛苦。
<script language="javascript" type="text/javascript">
Sys.Application.add_load(initializePage);
function initializePage() {
$find('PlantDropDown').add_propertyChanged(function(sender, e) {
if (e.get_propertyName() == 'selectedIndex') {
var newValue = sender.get_textBoxControl().value;
// persist selected value here...
}})
}
</script>https://stackoverflow.com/questions/1457813
复制相似问题