javascript 使用 jQuery 获取动态复选框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10856840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get dynamic checkbox values with jQuery
提问by MarwaInsat
I can't get the checked values of the dynamic check box. What am I doing wrong? http://jsfiddle.net/hxfsB/17/
我无法获取动态复选框的选中值。我究竟做错了什么?http://jsfiddle.net/hxfsB/17/
Markup:
标记:
<html>
<body>
one<input type="checkbox" name='checkbox0' value="one_name" checked>
two<input type="checkbox" name='checkbox1' value="one_name1">
three<input type="checkbox" name='checkbox2' value="one_name2">
<input type="button" id="envoyer" value="Envoyer Reponse" />
</body>
</html>
Javascript:
Javascript:
$('#envoyer').click(function(e){
var myArray=new Array(4);
for ( var j = 0; j < 3; j++){
var check=$('input:checkbox[name=checkbox'+j+']').is(':checked');
if(check==true)
myArray[j]=$('input:checkbox[name=checkbox'+j+']').val();
}
alert('check '+" "+myArray[i]);
});
采纳答案by dSquared
You had an Uncaught ReferenceError: i is not defined
; I've updated your fiddle here:
你有一个Uncaught ReferenceError: i is not defined
; 我在这里更新了你的小提琴:
$('#envoyer').click(function(e){
var myArray = new Array(3);
for ( var j = 0; j < 3; j++) {
var check=$('input:checkbox[name=checkbox'+j+']').is(':checked');
if(check==true)
myArray[j]=$('input:checkbox[name=checkbox'+j+']').val();
// Alert Current Selection //
alert('check ' + " " + myArray[j] );
}
});
Keep in mind: undefined
means the checkbox is NOT selected.
请记住:undefined
表示未选中该复选框。
I hope this helps!
我希望这有帮助!
回答by VisioN
You have an error when outputting myArray
in alert (there is no i
variable defined).
myArray
在警报中输出时出错(i
未定义变量)。
However, your code can be better structured. Here is one solution:
但是,您的代码可以更好地结构化。这是一种解决方案:
$("#envoyer").click(function(e) {
var myArray = [];
$(":checkbox:checked").each(function() {
myArray.push(this.value);
});
alert("Checked: " + myArray.join(","));
});?
回答by Jashwant
At your title suggests, if you want to get checked check-boxes values, you can do this,
根据您的标题建议,如果您想获得选中的复选框值,您可以这样做,
Javascript:
Javascript:
$('#envoyer').click(function(e){
$('input[type="checkbox"]:checked').each(function(){
alert(this.value);
})
})?
Markup
标记
<html>
<body>
one<input type="checkbox" name='checkbox0' value="one_name" checked>
two<input type="checkbox" name='checkbox1' value="one_name1">
three<input type="checkbox" name='checkbox2' value="one_name2">
<input type="button" id="envoyer" value="Envoyer Reponse" />
</body>
</html>
回答by frictionlesspulley
try this out : http://jsfiddle.net/hxfsB/27/
试试这个:http: //jsfiddle.net/hxfsB/27/
there is an issue with the selector
选择器有问题
回答by lucuma
$('#envoyer').click(function(e){
var myArray=new Array(3);
$('input[type=checkbox]:checked').each(function(i) {
myArray[i] = $(this).val();
//alert($(this).val());
});
}?);?
回答by Kay
The i
in myArray[i]
doesn't exist anywhere. You need to either put the alert inside the for loop and use myArray[j]
or create a new for loop using i
将i
在myArray[i]
任何地方都不会存在。您需要将警报放在 for 循环中并使用myArray[j]
或创建一个新的 for 循环使用i