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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:17:06  来源:igfitidea点击:

Get dynamic checkbox values with jQuery

javascriptjquerycheckbox

提问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; 我在这里更新了你的小提琴:

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: undefinedmeans the checkbox is NOT selected.

请记住:undefined表示未选中该复选框。

I hope this helps!

我希望这有帮助!

回答by VisioN

You have an error when outputting myArrayin alert (there is no ivariable 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(","));
});?

DEMO:http://jsfiddle.net/hxfsB/25/

演示:http : //jsfiddle.net/hxfsB/25/

回答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 iin 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

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