![](/img/trans.png)
[英]How to Get the value of the checked radio button in a group using type attribute?
[英]get the checked radio button in each radio group
此代码尝试获取每个单选组中选中的单选按钮中value
属性的value
。 但无法正常工作。 谢谢
let doc = {};
$('.radio-group').each(function () {
doc.id = $(this).attr('_id');
$(this).children.each(function () {
let validName = $($(this):checked).attr('value');
doc.checkedName = validName;
}
})
<div class='radio-group' _id='abc'>
<label class='radio-item'>
<input class='radio-icon' value='value 1' name='g' type='radio'></input>
<span class='radio-label'>choose me </span>
</label>
<label class='radio-item'>
<input class='radio-icon' value='value 2' name='g' type='radio'></input>
<span class='radio-label'>choose me </span>
</label>
</div>
使用$(".radio-group input:checked")
您可以选择每个.radio-group
子级的选中输入。
$(".radio-group input").click(function(){ var doc = []; $(".radio-group input:checked").each(function(index) { var id = $(this).closest(".radio-group").attr('_id'); doc.push(id); }); console.log(doc); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="radio-group" _id="id1"> <input type="radio" /> <label>Item 1</label> </div> <div class="radio-group" _id="id2"> <input type="radio" /> <label>Item 2</label> </div> <div class="radio-group" _id="id3"> <input type="radio" /> <label>Item 3</label> </div> <div class="radio-group" _id="id4"> <input type="radio" /> <label>Item 4</label> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.