简体   繁体   English

单击动态生成的单选按钮

[英]Click on dynamically generated radio button

i am trying to alert some text when a dynamically generated radio button is checked .. here is the link from fiddle .. http://jsfiddle.net/z7cu3q0y/ 我正在尝试检查动态生成的单选按钮时提醒一些文本..这是小提琴的链接.. http://jsfiddle.net/z7cu3q0y/

function createRadioButtons(n)
{
    $("#radioContainer").empty();
    for(var i=0;i<n;i++)
    {
        radioButtons = "<p><input type='radio' class='"+n+"' name='"+n+"'>"+(i+1)+"</p>";
        $("#radioContainer").append(radioButtons);
    }
}    

$("#dropDown").on("change",function()
{
      createRadioButtons(parseInt($(this).val()));
});

$("#radioContainer input").on("change",function()
{
      alert("checked");
});

when i click on radio button i am not getting alert .. can any one of you please help me in taking a look ? 当我单击单选按钮时,我没有收到警报。.你们中的任何一个可以帮助我看看吗?

Thanks in advance, Ashwin 在此先感谢,Ashwin

Your code $("#radioContainer input").on("change",function(){}) will directly attach the event handler to the matching elements that are currently present in DOM . 您的代码$("#radioContainer input").on("change",function(){})将事件处理程序直接附加到DOM中当前存在的匹配元素。

To work with dynamically generated elements added in the future, You need to delegate your event handler to a common parent element: 要使用将来添加的动态生成的元素,您需要事件处理程序委派给一个公共的父元素:

$("#radioContainer").on("change","input",function(){
  alert("checked");
});

The above will attach the handler to #radioContainer , Whenever the corresponding event ( change in this case )is triggered ( Usually propagated from the children ), it checks whether the event target matches the specified selector ( input in this case) and invokes the handler accordingly. 上面的代码会将处理程序附加到#radioContainer ,每当触发相应的事件( 在这种情况下change )( 通常从子级传播 ),它都会检查事件目标是否与指定的选择器(在这种情况下为input )匹配并调用处理程序相应地。

Updated Fiddle 更新小提琴

You need to use .on() for dynamically generated elements like below. 您需要将.on()用于动态生成的元素,如下所示。 Here .on() will bind change event to all radio buttons which are inside radioContainer 在此.on()会将change事件绑定到radioContainer内部的所有单选按钮

$("#radioContainer").on("change","input[type=radio]",function()
{
      alert("checked");
});

Demo 演示版

Try using the below code 尝试使用以下代码

$(document).on("change","#radioContainer input",function(){
 alert("checked");
});

Updated fiddle 更新的小提琴

You need to put the input defined as click area and radiocontainer as working area. 您需要将定义为单击区域的输入和将radiocontainer定义为工作区域。

DEMO: http://jsfiddle.net/don/z7cu3q0y/3/ 演示: http : //jsfiddle.net/don/z7cu3q0y/3/

Just remove input before .on and put inside of the function. 只需删除.on之前的输入并将其放在函数内部即可。


jQuery: jQuery的:

function createRadioButtons(n)
{
    $("#radioContainer").empty();
    for(var i=0;i<n;i++)
    {
        radioButtons = "<p><input type='radio' class='"+n+"' name='"+n+"'>"+(i+1)+"</p>";
        $("#radioContainer").append(radioButtons);
    }
}    

$("#dropDown").on("change",function()
{
      createRadioButtons(parseInt($(this).val()));
});

$("#radioContainer").on("change", 'input', function()
                              {
                                  alert("checked");
                              });

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

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