首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在文本框更改时将Javascript事件处理程序绑定到组合框

如何在文本框更改时将Javascript事件处理程序绑定到组合框
EN

Stack Overflow用户
提问于 2009-09-22 02:09:10
回答 2查看 9K关注 0票数 4

我需要一个用于ASP.NET项目的组合框,所以我决定使用Ajax (http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ComboBox/ComboBox.aspx)。

我不想使用回发,因为我不想重新加载页面,但是我需要知道文本框中的文本何时被更改,这样我就可以调用服务器来持久化新的列表项。

我很好奇如何将onchangeonblur事件绑定到这个组合框使用的输入框。

这是我的asp.net页面:

代码语言:javascript
运行
复制
<%@ 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文件中有了如下内容:

代码语言:javascript
运行
复制
<input type="hidden" id="PlantDropDownID" value="<%= PlantDropDown.ClientID %>" />

这是在我的javascript文件中,因为我不把javascript推到我的.aspx文件中:

代码语言:javascript
运行
复制
elem = document.getElementById('PlantDropDownID');
$find(elem.value).add_propertyChanged(function(sender, e) {
    if (e.get_propertyName() == 'selectedIndex') {
        var newValue = sender.get_textBoxControl().value;
    }
})
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-09-22 02:48:00

我相信您应该绑定到"propertyChanged"事件并检查对"selectedIndex"属性的更改:

代码语言:javascript
运行
复制
$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。要获得引用,您可能需要:

代码语言:javascript
运行
复制
$find('<%= PlantDropDown.ClientID %>')

我使用jQuery作为javascript端

那是没有意义的。

票数 9
EN

Stack Overflow用户

发布于 2009-11-11 08:41:52

我发现,除非我将其包装成如下所示的函数,否则我无法获得所提供的答案。不知道这是为什么,但希望它能给别人一些痛苦。

代码语言:javascript
运行
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1457813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档