[英]Get dynamic checkbox values with jQuery
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]);
});
You have an error when outputting myArray
in alert (there is no i
variable defined). 在alert中输出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(","));
});
DEMO: http://jsfiddle.net/hxfsB/25/ 演示: http : //jsfiddle.net/hxfsB/25/
You had an Uncaught ReferenceError: i is not defined
; 你有一个Uncaught ReferenceError: i is not defined
; I've updated your fiddle here: 我在这里更新了你的小提琴:
http://jsfiddle.net/hxfsB/24/ http://jsfiddle.net/hxfsB/24/
$('#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! 我希望这有帮助!
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>
try this out : http://jsfiddle.net/hxfsB/27/ 试试这个: http : //jsfiddle.net/hxfsB/27/
there is an issue with the selector 选择器存在问题
$('#envoyer').click(function(e){
var myArray=new Array(3);
$('input[type=checkbox]:checked').each(function(i) {
myArray[i] = $(this).val();
//alert($(this).val());
});
});
The i
in myArray[i]
doesn't exist anywhere. myArray[i]
中的i
在任何地方都不存在。 You need to either put the alert inside the for loop and use myArray[j]
or create a new for loop using i
您需要将警报放在for循环中并使用myArray[j]
或使用i
创建一个新的for循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.