2014-02-06 65 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:另外,請保持名稱相同,以便一次只選擇一個無線電。

乾杯, 阿肖克