jquery checkbox 构建一个数组,其中包含带有 id 的已更改复选框列表

jquery checkbox build an array containing a list of changed checkbox with the id

我有一个 table 基于以下 json 文件

[
{"id":"1","rank":"9","content":"Alon","UID":"5","P1":"1","P2":"0","P3":"0","P4":"1","P5":"0"},
{"id":"2","rank":"6","content":"Tala","UID":"6","P1":"1","P2":"0","P3":"0","P4":"1","P5":"0"},
{"id":"3","rank":"9","content":"Alon","UID":"5","P1":"1","P2":"0","P3":"0","P4":"1","P5":"0"},
{"id":"4","rank":"6","content":"Tala","UID":"6","P1":"1","P2":"0","P3":"0","P4":"1","P5":"0"},
{"id":"5","rank":"6","content":"Tala","UID":"6","P1":"1","P2":"0","P3":"0","P4":"1","P5":"0"}]

然后使用jquery填充table。

$.ajax({
    url: 'test.json',
    type: 'POST',
    success: function (response) {
        var trHTML = '';
        var valueOn = 1;
        var valueOff = 0;

        $.each(response, function (i, item) {
            trHTML += '<tr><td>' + item.rank + '</td><td>' + item.content + '</td><td>' + item.UID + '</td><td>' + '<input type="checkbox" value="' + item.P1 + '"></td><td>' + '<input type="checkbox" value="' + item.P2 + '"></td><td>' + '<input type="checkbox" value="' + item.P3 + '"></td><td>' + '<input type="checkbox" value="' + item.P4 + '"></td><td>' + '<input type="checkbox" value="' + item.P5 + '"></td></tr>';            
        });
        $('#records_table').append(trHTML);

        $('input[type="checkbox"]').each(function () {
            if ( $(this).val() == valueOn ) {
                $(this).attr("checked",true);
            }                     
            if ( $(this).val() == valueOff ) {
                $(this).attr("checked",false);
            }            
        });

        $('tr:odd').css("background", "green");
    },
    error: function (response) {
        console.log('shit shit shit!!!! now see me running!');
    },

});

$('#sendInfo').click(function() {
    var listOfCheckboxes = [];

});

我试图实现的是发送或创建一个包含 id 和值 chekbox 的字符串或数组,类似于这样 {Id : 1, { P1: 1 , P2 : 0 , P3 1 , P4 : 0 , P4: 0 }}.

我尝试用这个做的基本上是知道那个人拥有改变的值。

希望我正确解释了您的问题。如果你想获得 P1, P2, ect. 你可以设置 value='P1' 然后在你的 php 提交页面上你可以通过做类似 if($_REQUEST['P1'] === 'Yes') 的事情来了解 P1 是否被检查 复选框将 return yes 如果勾选,no 如果不勾选。

然后另一方面,如果你想查看是否在 HTML 页面上检查了值。您将使用这样的 $.each 语句。

JSFiddle

var a = [{
    "id": "1",
    "rank": "9",
    "content": "Alon",
    "UID": "5",
    "P1": "1",
    "P2": "0",
    "P3": "0",
    "P4": "1",
    "P5": "0"
}, {
    "id": "2",
    "rank": "6",
    "content": "Tala",
    "UID": "6",
    "P1": "1",
    "P2": "0",
    "P3": "0",
    "P4": "1",
    "P5": "0"
}, {
    "id": "3",
    "rank": "9",
    "content": "Alon",
    "UID": "5",
    "P1": "1",
    "P2": "0",
    "P3": "0",
    "P4": "1",
    "P5": "0"
}, {
    "id": "4",
    "rank": "6",
    "content": "Tala",
    "UID": "6",
    "P1": "1",
    "P2": "0",
    "P3": "0",
    "P4": "1",
    "P5": "0"
}, {
    "id": "5",
    "rank": "6",
    "content": "Tala",
    "UID": "6",
    "P1": "1",
    "P2": "0",
    "P3": "0",
    "P4": "1",
    "P5": "0"
}];
var trHTML = '';
$.each(a, function (i, item) {
    trHTML += '<tr><td>' 
    + item.rank 
    + '</td><td>' 
    + item.content 
    + '</td><td>' 
    + item.UID 
    + '</td><td>' 
    + '<input type="checkbox" value="P1" '+check(item.P1)+'></td><td>' 
    + '<input type="checkbox" value="P2" '+check(item.P2)+'></td><td>' 
    + '<input type="checkbox" value="P3" '+check(item.P3)+'></td><td>' 
    + '<input type="checkbox" value="P4" '+check(item.P4)+'></td><td>' 
    + '<input type="checkbox" value="P5" '+check(item.P5)+'></td></tr>';
});
$('#records_table').append(trHTML);

function check(value){
    if(value == 1){
        return 'checked';
    }
}

$('tr:odd').css("background", "yellow");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id='records_table'></table>