简体   繁体   English

JQuery 下拉列表/选择 onchange 事件未触发

[英]JQuery dropdownlist/select onchange event not fired

I have this dropodownlist / combobox / select element inside an asp.net form:我在 asp.net 表单中有这个下拉列表 / combobox / select 元素:

<asp:DropDownList 
 ID="_ddlMode" 
 runat="server" 
 AppendDataBoundItems="true" 
 ClientIDMode="Static" 
 CssClass="input_medium" 
 DataSourceID="_sdsModes" 
 DataTextField="description" 
 DataValueField="id">
 <asp:ListItem Selected="True" Text="None" Value="" />
</asp:DropDownList>

I need to call function SetModes() when the selected element is changed from the user.当从用户更改所选元素时,我需要调用 function SetModes() I tried with this:我试过这个:

 $(function () {
   $(document.body).on('change', '_ddlMode', function () {
       var _index = $("select[id='_ddlMode'] option:selected").index();
       SetModes(_txtBank, _txtZip, _index);
   });
 });

No luck.没运气。 Then I tried this:然后我尝试了这个:

 $(function () {
    _ddlMode= $("input[id$='_ddlMode']");

    _ddlMode.change(
        function () {
           var _index = $("select[id='_ddlMode'] option:selected").index();
           SetModes(_txtBank, _txtZip, _index);
        }
    );
});

Again no luck.... where's the error?再次没有运气....错误在哪里? There's a more efficent way to retrieve the index of the selected element within the event handling?在事件处理中检索所选元素的索引有更有效的方法吗?

PS I'm using jquery-.2.1.3 , jquery-ui-1.11.2 PS我正在使用jquery-.2.1.3jquery-ui-1.11.2

So, you are willing to hit a method in your javascript code and it is not working for you.因此,您愿意在javascript代码中使用一种方法,但它对您不起作用。 There could be many reasons behind why it is not hitting it, I would write few of those possible reasons here with their respective possible solution.为什么它没有击中它可能有很多原因,我会在这里写一些可能的原因以及它们各自可能的解决方案。

Reason:原因:
Since you are using jQuery to bind the dropdownlist with a change event, I would suspect if your jQuery is being referenced correctly.由于您使用 jQuery 将下拉列表与更改事件绑定,因此我怀疑您的 jQuery 是否被正确引用。

Solution:解决方案:
Go to the browser's dev tools' console tab ( CTRL + SHIFT + I ) and see if you have any error with jQuery not being loaded, If yes then fix it. Go 到浏览器的开发工具的console选项卡( CTRL + SHIFT + I ),看看你是否有任何错误 jQuery 没有被加载,如果是,那么修复它。


Reason:原因:
As @freedomn-m has pointed out in the comments, You are not specifying a css selector, in your case # before your id while binding the event.正如@freedomn-m 在评论中指出的那样,您没有指定 css 选择器,在您的情况下#在绑定事件时在您的id之前。

Solution:解决方案:

$(function () {
   $(document.body).on('change', '#_ddlMode', function () {
       var _index = $("select[id='_ddlMode'] option:selected").index();
       SetModes(_txtBank, _txtZip, _index);
   });
 });

Reason:原因:
Your event is not getting bound to the desired control.您的事件未绑定到所需的控件。 This could be very specific to ASP.NET Web Forms, as you can face it while you are building your web apps.这可能非常特定于ASP.NET Web Forms,因为您可以在构建 Z2567DZ4 应用程序时面对它。

So, technically, ASP.NET can modify the IDs of your controls when you compile them and it could be a possible reason for not hitting the method.因此,从技术上讲, ASP.NET可以在您编译控件时修改它们的 ID,这可能是未命中该方法的可能原因。

For example, if you have a ContentPlaceHolder with an id of cph_1 and have markup in it as your child page, then ASP.NET will concatenate that id with every nested server control of yours.例如,如果您有一个idcph_1ContentPlaceHolder并在其中将标记作为您的子页面,那么ASP.NET会将该 ID 与您的每个嵌套服务器控件连接起来。 So, if you have a textbox with an id of tb1 in that content place holder, it would get modified to cph1_tb1 when browsing through that page.因此,如果您在该内容占位符中有一个idtb1textbox ,则在浏览该页面时它将被修改为cph1_tb1

Solution:解决方案:
Bind your event with the help of this syntax ( <%%> ) like below:借助此语法 ( <%%> ) 绑定您的事件,如下所示:

$(function () {
   $(document.body).on('change', '#<%= _ddlMode.ClientID %>', function () {
       var _index = $("select[id='<%= _ddlMode.ClientID %>'] option:selected").index();
       SetModes(_txtBank, _txtZip, _index);
   });
 });

See, if you can resolve the issue with these solutions.看看,如果你能用这些解决方案解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM