![](/img/trans.png)
[英]How to get radio button selected value from dynamically generated radio buttons?
[英]jQuery dynamically generated radio buttons then value is passed to continue button
我有一個接收 JSON 響應的頁面。 如果 JSON 中有多個選項,它將根據更大的長度使用 Javascript 中的 for 循環動態生成一些單選按鈕。
就像是:
for (var i = 0; i < length; i ++){
alert(i);
//draw the HTML radio buttons based on the data i++
}
//value from radio button button gets passed to a continue button
在 jQuery 中做到這一點的最佳方法是什么?
就像是
for ( var i in json ) {
var input = $('<input>', {
type: 'radio', name: 'group-name',
value: json[i],
'class': 'my_radio'
} );
$('body').append( input );
}
$('#continue').click( function() {
var selected = null;
$('input.my_radio').each( function() {
if ( $(this).attr( 'selected' ) )
selected = $(this).val();
});
// do something with selected....
});
如果需要,您必須用表單元素包圍它。 順便說一下,我還沒有測試過這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.