jQuery 使用复选框名称获取复选框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14192522/
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-08-26 13:20:56  来源:igfitidea点击:

Get checkbox values using checkbox name

jqueryhtml

提问by Oto Shavadze

I have several input checkboxes, (they name is same for send array on server).

我有几个输入复选框,(它们的名称与服务器上的发送数组相同)。

So, I need get each value this checkboxes and I want use as selector checkbox names, this not works, help please.

所以,我需要获取这个复选框的每个值,我想用作选择器复选框名称,这不起作用,请帮忙。

<form>
  <input type="checkbox" name="bla[]" value="1" />
  <input type="checkbox" name="bla[]" value="2" />
</form>

js:

js:

$(document).ready( function () {    

   $("input[name=bla]").each( function () {
       alert( $(this).val() );
   });

});

DEMO

演示

回答by undefined

You are selecting inputs with name attribute of "bla", but your inputs have "bla[]"name attribute.

您正在选择 name 属性为"bla"的输入,但您的输入具有"bla[]"name 属性。

$("input[name='bla[]']").each(function (index, obj) {
        // loop all checked items
    });

http://jsfiddle.net/26axX/

http://jsfiddle.net/26axX/

回答by rbtLong

You should include the brackets as well . . .

您还应该包括括号。. .

<input type="checkbox" name="bla[]" value="1" />

therefore referencing it should be as be name='bla[]'

因此引用它应该是 name='bla[]'

$(document).ready( function () { 

   $("input[name='bla[]']").each( function () {
       alert( $(this).val() );
   });

});

回答by André

If you like to get a list of all values of checked checkboxes (e.g. to send them as a list in one AJAX call to the server), you could get that list with:

如果您想获取已选中复选框的所有值的列表(例如,在一次 AJAX 调用中将它们作为列表发送到服务器),您可以通过以下方式获取该列表:

var list = $("input[name='bla[]']:checked").map(function () {
    return this.value;
}).get();

回答by Parameswaran

$('[name="CheckboxName"]:checked').each(function () {
    // do stuff
});

回答by Bardicer

I would like to add that if you're trying to get all of the check-boxes with that name that have been selected, you would want to use the following syntax:

我想补充一点,如果您尝试获取已选择的具有该名称的所有复选框,则需要使用以下语法:

$("[name='bla[]']:checked").each(function () {
    // do stuff
});

Make sure there is not a space between the closing ]and :checked

确保关闭]和关闭之间没有空间:checked

回答by Dirty-flow

$("input[name='bla[]']").each( function () {
    alert($(this).val());
});

回答by realshadow

Like it has been said few times, you need to change your selector to

就像已经说过几次一样,您需要将选择器更改为

$("input[name='bla[]']")

But I want to add, you haveto use single or double quotes when using [] in selector.

但我想补充一点,在选择器中使用 [] 时必须使用单引号或双引号。

回答by swamy

// get checkbox values using checkbox's name

// 使用复选框名称获取复选框值

<head>
        <script>
        function getCheckBoxValues(){
            $('[name="checkname"]').each( function (){
                alert($(this).val());
            });
        }
        </script>
    </head>
    <body>
        <input type="checkbox" name="checkname" value='1'/>
        <input type="checkbox" name="checkname" value='2'/>
        <input type="checkbox" name="checkname" value='3'/>
        <input type="button" value="CheckBoxValues" onclick="getCheckBoxValues()"/>
    </body>

// get only the values witch are checked

// 只获取被检查的值

function getCheckBoxValues(){
        $('[name="checkname"]').each( function (){
            if($(this).prop('checked') == true){
                alert($(this).val());
            }
        });
    }