如何使用jquery获取多个复选框值

use*_*007 53 jquery

如何获取使用jquery选中的复选框的值?我的HTML代码如下:

<input  id="ad_Checkbox1" class="ads_Checkbox" type="checkbox" value="1" />
<input  id="ad_Checkbox2" class="ads_Checkbox" type="checkbox" value="2" />
<input  id="ad_Checkbox3" class="ads_Checkbox" type="checkbox" value="3" />
<input  id="ad_Checkbox4" class="ads_Checkbox" type="checkbox" value="4" />
<input type="button" id="save_value" name="save_value" value="Save" />
Run Code Online (Sandbox Code Playgroud)

Jui*_*ice 103

试试这个

<input name="selector[]" id="ad_Checkbox1" class="ads_Checkbox" type="checkbox" value="1" />
<input name="selector[]" id="ad_Checkbox2" class="ads_Checkbox" type="checkbox" value="2" />
<input name="selector[]" id="ad_Checkbox3" class="ads_Checkbox" type="checkbox" value="3" />
<input name="selector[]" id="ad_Checkbox4" class="ads_Checkbox" type="checkbox" value="4" />
<input type="button" id="save_value" name="save_value" value="Save" />
Run Code Online (Sandbox Code Playgroud)

功能

    $(function(){
      $('#save_value').click(function(){
        var val = [];
        $(':checkbox:checked').each(function(i){
          val[i] = $(this).val();
        });
      });
    });
Run Code Online (Sandbox Code Playgroud)


dug*_*tov 61

要从多个选中的复选框中获取值数组,请使用jQuery map/get函数:

$('input[type=checkbox]:checked').map(function(_, el) {
    return $(el).val();
}).get();
Run Code Online (Sandbox Code Playgroud)

这将返回带有选中值的数组,如下所示: ['1', '2']

这是jsfiddle的工作示例:http://jsfiddle.net/7PV2e/


Adi*_*dil 19

你可以这样做,

$('.ads_Checkbox:checked')
Run Code Online (Sandbox Code Playgroud)

您可以each()使用复选框值迭代它们并填充数组.

现场演示

获取数组中所选复选框的值

var i = 0;
$('#save_value').click(function () {
       var arr = [];
       $('.ads_Checkbox:checked').each(function () {
           arr[i++] = $(this).val();
       });      
});
Run Code Online (Sandbox Code Playgroud)

编辑,使用.map()

您还可以使用jQuery.mapget()获得所选checkboxe值的数组.

作为旁注使用this.value而不是$(this).val()会提供更好的性能.

现场演示

$('#save_value').click(function(){
    var arr = $('.ads_Checkbox:checked').map(function(){
        return this.value;
    }).get();
}); 
Run Code Online (Sandbox Code Playgroud)


Jon*_*eet 7

$('input:checked').map(function(i, e) {return e.value}).toArray();
Run Code Online (Sandbox Code Playgroud)


rah*_*hul 6

你可以像这样得到它们

$('#save_value').click(function() {
    $('.ads_Checkbox:checked').each(function() {
        alert($(this).val());
    });
});
Run Code Online (Sandbox Code Playgroud)

提琴手


Dev*_*per 6

试试这个,jQuery 如何获取多个复选框的值

对于演示或更多示例

    $(document).ready(function() {
        $(".btn_click").click(function(){
            var test = new Array();
            $("input[name='programming']:checked").each(function() {
                test.push($(this).val());
            });
 
            alert("My favourite programming languages are: " + test);
        });
    });
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Values of Selected Checboxes</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
</head>
<body>
    <form>
        <h3>Select your favorite Programming Languages :</h3>
        <label><input type="checkbox" value="PHP" name="programming"> PHP</label>
        <label><input type="checkbox" value="Java" name="programming"> Java</label>
        <label><input type="checkbox" value="Ruby" name="programming"> Ruby</label>
        <label><input type="checkbox" value="Python" name="programming"> Python</label>
        <label><input type="checkbox" value="JavaScript" name="programming"> JavaScript</label>
        <label><input type="checkbox" value="Rust" name="programming">Rust</label>
        <label><input type="checkbox" value="C" name="programming"> C</label>
        <br>
        <button type="button" class="btn_click" style="margin-top: 10px;">Click here to Get Values</button>
    </form>
</body>
</html>  
Run Code Online (Sandbox Code Playgroud)