如何使用Jquery在数组中存储所选复选框的值

时间:2018-03-17 07:59:11

标签: jquery html

我有一组带有指定值的复选框,我想使用JQuery将所选复选框的值存储在数组中。

我该怎么办?

<table id="user" class="w3-table-all w3-hoverable w3-card-4 display">
    <thead>
            <tr class="w3-light-grey">
                        <th><input type="checkbox"" class="w3-check" id="select-all" /></th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Points</th>
            </tr>

    </thead>
                <tbody>
                    <tr>
                        <td><input type="checkbox" class="w3-check checkbox" value="1" /></td>
                        <td>Jill</td>
                        <td>Smith</td>
                        <td>50</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" class="w3-check checkbox" value="2" /></td>
                        <td>Eve</td>
                        <td>Jackson</td>
                        <td>94</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" class="w3-check checkbox" value="3" /></td>
                        <td>Adam</td>
                        <td>Johnson</td>
                        <td>67</td>
                    </tr>
                </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

$(document).ready(function() {
    //set initial state.
    var arr =[]

    $('.w3-check').mousedown(function() {
        if (!$(this).is(':checked')) {
            arr.push($(this).val())
            
             document.getElementById("asdf").innerHTML = arr;
            
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table id="user" class="w3-table-all w3-hoverable w3-card-4 display">
    <thead>
            <tr class="w3-light-grey">
  <th><input type="checkbox" class="w3-check"  id="select-all" /></th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Points</th>
            </tr>

    </thead>
                <tbody>
                    <tr>
      <td><input type="checkbox" class="w3-check" value="1" /></td>
                        <td>Jill</td>
                        <td>Smith</td>
                        <td>50</td>
                    </tr>
                    <tr>
    <td><input type="checkbox" class="w3-check" value="2" /></td>
                        <td>Eve</td>
                        <td>Jackson</td>
                        <td>94</td>
                    </tr>
                    <tr>
 <td><input type="checkbox" class="w3-check" value="3" /></td>
                        <td>Adam</td>
                        <td>Johnson</td>
                        <td>67</td>
                    </tr>
                </tbody>
</table>
<p id='asdf'>Arr DATA</p>