jquery 具有多个相似组的已检查单选组的值
jquery value from checked radio group with numerous similar groups
我有数量可变的类似无线电组,我需要通过 class 依次循环它们并输出选定的值。 JS Fiddle 下面。我意识到我可以通过使用 #ID 而不是 .Class 更直接地找到它,但是没有所有细节,我需要走这条路。
在创建问题的过程中,我发现 b 数组的长度是 9,所以为了得到第二组,我使用 b[2*(# radios in group)],但这似乎有点老套.还有其他推荐吗?
<div id="1">
<input type = "radio" name="a0" value="1" class="a" checked="checked" >1</input>
<input type = "radio" name="a0" value="2" class="a">2</input>
<input type = "radio" name="a0" value="3" class="a">3</input>
<div>
<div id="2">
<input type = "radio" name="a1" value="1" class="a">1</input>
<input type = "radio" name="a1" value="2" class="a" checked="checked">2</input>
<input type = "radio" name="a1" value="3" class="a">3</input>
<div>
<div id="3">
<input type = "radio" name="a2" value="1" class="a">1</input>
<input type = "radio" name="a2" value="2" class="a">2</input>
<input type = "radio" name="a2" value="3" class="a" checked="checked">3</input>
<div>
<button onclick="getValue();">Get values</button>
<script>
function getValue() {
var b = $(".a");
alert( $('input:radio[name=' + $(b[2*3]).attr("name") + ']:checked').val() );
}
</script>
您可以使用jQuery 的$.each
函数进行迭代。
function getValue() {
$("input:radio.a:checked").each(function () {
console.log(this);
});
}
见
如果您对检查输入的数组没意见:
// iterate over the <input> elements of
// class 'a' and which are checked,
// forming a map:
var checked = $('input.a:checked').map(function () {
// return the value of the current <input>
// of the collection of elements:
return this.value;
// convert the map to an Array:
}).get();
console.log(checked); // ["1", "2", "3"];
如果您更愿意生成一个对象,其中名称是键,输入的值是键值:
// create an Object:
var checked = {};
// iterate, using each(), over the checked
// inputs of class-name 'a':
$('input.a:checked').each(function () {
// setting a property (this.name) on
// the named Object (checked), and
// setting its value to the <input>
// value (this.value):
checked[this.name] : this.value;
});
console.log(checked); // { 'a0' : "1", 'a1' : "2", 'a2' : "3" }
参考文献:
我发现找到封闭的 div 更简单一些,每个 div 都包含单选组和 select 如:
第二组:
$(b[1]).children('input[type=radio]:checked').val()
包含一个 JSFiddle。 https://jsfiddle.net/cycle4passion/6c0q0kum/5/
我有数量可变的类似无线电组,我需要通过 class 依次循环它们并输出选定的值。 JS Fiddle 下面。我意识到我可以通过使用 #ID 而不是 .Class 更直接地找到它,但是没有所有细节,我需要走这条路。
在创建问题的过程中,我发现 b 数组的长度是 9,所以为了得到第二组,我使用 b[2*(# radios in group)],但这似乎有点老套.还有其他推荐吗?
<div id="1">
<input type = "radio" name="a0" value="1" class="a" checked="checked" >1</input>
<input type = "radio" name="a0" value="2" class="a">2</input>
<input type = "radio" name="a0" value="3" class="a">3</input>
<div>
<div id="2">
<input type = "radio" name="a1" value="1" class="a">1</input>
<input type = "radio" name="a1" value="2" class="a" checked="checked">2</input>
<input type = "radio" name="a1" value="3" class="a">3</input>
<div>
<div id="3">
<input type = "radio" name="a2" value="1" class="a">1</input>
<input type = "radio" name="a2" value="2" class="a">2</input>
<input type = "radio" name="a2" value="3" class="a" checked="checked">3</input>
<div>
<button onclick="getValue();">Get values</button>
<script>
function getValue() {
var b = $(".a");
alert( $('input:radio[name=' + $(b[2*3]).attr("name") + ']:checked').val() );
}
</script>
您可以使用jQuery 的$.each
函数进行迭代。
function getValue() {
$("input:radio.a:checked").each(function () {
console.log(this);
});
}
见
如果您对检查输入的数组没意见:
// iterate over the <input> elements of
// class 'a' and which are checked,
// forming a map:
var checked = $('input.a:checked').map(function () {
// return the value of the current <input>
// of the collection of elements:
return this.value;
// convert the map to an Array:
}).get();
console.log(checked); // ["1", "2", "3"];
如果您更愿意生成一个对象,其中名称是键,输入的值是键值:
// create an Object:
var checked = {};
// iterate, using each(), over the checked
// inputs of class-name 'a':
$('input.a:checked').each(function () {
// setting a property (this.name) on
// the named Object (checked), and
// setting its value to the <input>
// value (this.value):
checked[this.name] : this.value;
});
console.log(checked); // { 'a0' : "1", 'a1' : "2", 'a2' : "3" }
参考文献:
我发现找到封闭的 div 更简单一些,每个 div 都包含单选组和 select 如:
第二组:
$(b[1]).children('input[type=radio]:checked').val()
包含一个 JSFiddle。 https://jsfiddle.net/cycle4passion/6c0q0kum/5/