使用jQuery获取动态复选框值。

时间:2022-12-02 17:32:48

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]);
});

6 个解决方案

#1


3  

You had an Uncaught ReferenceError: i is not defined; I've updated your fiddle here:

你有一个未捕获的ReferenceError: i没有定义;我更新了你的小提琴:

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!

我希望这可以帮助!

#2


5  

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/

#3


1  

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>

#4


0  

try this out : http://jsfiddle.net/hxfsB/27/

试试这个:http://jsfiddle.net/hxfsB/27/。

there is an issue with the selector

选择器有一个问题

#5


0  

$('#envoyer').click(function(e){
    var myArray=new Array(3);

    $('input[type=checkbox]:checked').each(function(i) {
       myArray[i] = $(this).val();
       //alert($(this).val());
    });
}​);​

#6


0  

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

myArray中的i在任何地方都不存在。您需要将警报放入for循环中并使用myArray[j]或使用i创建一个新的for循环

#1


3  

You had an Uncaught ReferenceError: i is not defined; I've updated your fiddle here:

你有一个未捕获的ReferenceError: i没有定义;我更新了你的小提琴:

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!

我希望这可以帮助!

#2


5  

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/

#3


1  

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>

#4


0  

try this out : http://jsfiddle.net/hxfsB/27/

试试这个:http://jsfiddle.net/hxfsB/27/。

there is an issue with the selector

选择器有一个问题

#5


0  

$('#envoyer').click(function(e){
    var myArray=new Array(3);

    $('input[type=checkbox]:checked').each(function(i) {
       myArray[i] = $(this).val();
       //alert($(this).val());
    });
}​);​

#6


0  

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

myArray中的i在任何地方都不存在。您需要将警报放入for循环中并使用myArray[j]或使用i创建一个新的for循环