用于 asp.net radiobuttonlist 的 JQuery 'Change' 事件处理程序不触发事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1261259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:01:54  来源:igfitidea点击:

JQuery 'Change' event handler for asp.net radiobuttonlist not triggering event

asp.netjqueryjavascript-eventsevent-handling

提问by Tony_Henrich

I wired the Change event handler for an ASP.NET radiobuttonlist like this in the ready() handler in JQuery like this:

我在 JQuery 的 ready() 处理程序中为这样的 ASP.NET radiobuttonlist 连接了 Change 事件处理程序,如下所示:

$("#<%=rblYesNo.ClientID%>").change(MyFunction);

When I select one of the radio buttons, MyFunction doesn't get called. Why?

当我选择其中一个单选按钮时,不会调用 MyFunction。为什么?

回答by aquinas

Remember, a radio button list doesn't have a single identifier. The radio buttons are linked together by their NAME. If I recall, rblYesNo.ClientID will probably be just a div that wraps the radio buttons. Try:

请记住,单选按钮列表没有单一标识符。单选按钮通过它们的名称链接在一起。如果我记得, rblYesNo.ClientID 可能只是一个包装单选按钮的 div。尝试:

$("#<%=rblYesNo.ClientID%> input").change(function(){

});

回答by karim79

IE has a problem with the 'change' event on radio buttons, try using click instead:

IE 在单选按钮上的 'change' 事件有问题,请尝试改用 click:

$("#<%=rblYesNo.ClientID%>").click(MyFunction);

回答by DineshHona

$(document).ready(function() {
  $('#<%=rblYesNo.ClientID%> input[type="radio"]').each(function() {
                $(this).click(function() {
                alert((this).value);
            });
  });
 });

回答by DineshHona

$("#<%=rblYesNo.ClientID%> input").change(function(){ });

$("#<%=rblYesNo.ClientID%> input").change(function(){ });

and

$("#<%=rblYesNo.ClientID%>").click(MyFunction);

$("#<%=rblYesNo.ClientID%>").click(MyFunction);

it may works in simple page. what if there is AjaxControlToolkit TabPanel in page? it will not works. Because radio button list will be on other tab so it will find by the jquery and event cannot registered.

它可能适用于简单的页面。如果页面中有 AjaxControlToolkit TabPanel 怎么办?它不会工作。因为单选按钮列表将在其他选项卡上,所以它会被 jquery 找到并且无法注册事件。