2014-02-06 37 views
0

林将一个容器内几个单选按钮,动态地使用jQuery添加方法和添加的事件处理程序,所有的单选按钮,但它不工作事件代表团jQuery的

问:

每当我点击单选按钮在容器内,我需要一组要被执行的命令的..

fiddle- http://jsfiddle.net/Z86kW/1/

Jquery的

$(document).ready(function(){ 
    $('#container').append('<input type="radio" name="radio1"/>'); 
    $('#container').append('<input type="radio" name="radio2"/>'); 
    $('#container').append('<input type="radio" name="radio3"/>'); 


    $("#container").on("click", "radio", function(event) { 

      alert($(this).text()); 
      }) 
}) 

CSS

#container{ 
    height:100px; 
    width:200px; 
    background-color:red; 
} 

HTML

<div id="container"> 

</div> 
+0

,如果你打算使用它就像一个没有无线电集团(同名)复选框,你或许应该使用复选框。 – adeneo

+1

下面是我怎么做 - > http://jsfiddle.net/Z86kW/7/ – adeneo

回答

3

你的选择是不正确,你错过了:与选择

$("#container").on("click", ":radio", function(event) { 

Fiddle DEMO

$(":radio")相当于$("[type=radio]")

+0

感谢它的工作......我应该使用“:”为单选按钮或所有对象? – user1050619

+0

@ user1050619,'$(“:radio”)'相当于'$(“[type = radio]”)''。您需要提供正确的选择器。你指的是什么? – Satpal

+0

@ Satpal ..谢谢......我指的是这个文档 - https://learn.jquery.com/events/event-delegation/...当他选择所有“a”元素时,他并没有使用它..有什么区别.. – user1050619

0

在这里你想选择所有类型的无线电元件,那么你必须使用:radio选择器。

$("#container").on("click", ":radio", function(event) { 
    alert($(this).text()); 
}); 

Fiddle

,或者您也可以使用input[type=radio]

$("#container").on("click", "input[type=radio]", function(event) { 
    alert($(this).text()); 
}); 

Fiddle

0

我捏捏jQuery代码。请参考urlhttp://jsfiddle.net/Z86kW/9/

PS:另外,请保持名称相同,以便一次只选择一个无线电。

干杯, 阿肖克