【问题标题】:Get the Checked and Unchecked table data via jquery通过 jquery 获取 Checked 和 Unchecked 表数据
【发布时间】:2014-01-24 00:30:32
【问题描述】:

我正在使用 Jquery 在运行时创建一个表并将唯一 id 绑定到复选框。

  $.getJSON('/api/Test/SelectionList' + '/' + ID)
   .done(function (data) {          
       $.each(data, function (key, val) {
           var myRow = $("<tr/>");
           //$("<td> <input type='checkbox' ></input>   </td>").text(val.IsActive).appendTo($(myRow));
           var items = "";
           items += '<input type="checkbox" id=' + val.FacilityID + ' ';             
           if (val.IsSelected) {
               items += 'checked/>';
           }
           else {
               items += '/>';
           }
           //$("<td/>").text(val.IsActive).appendTo($(myRow));
           $("<td> " + items + "</td>").appendTo($(myRow));
           $("<td/>").text(val.Facilityname).appendTo($(myRow));
           $("<td/>").text(val.RegionName).appendTo($(myRow));
           $("<td/>").appendTo($(myRow));
           myRow.appendTo($("#Table"));
       });

   })

用户可以选中和取消选中复选框,单击保存时,我想存储(表)所有复选框的值,并带有 ID 的选中/未选中状态。 我想遍历整个表,并将数据存储为 id@1 用于选中的框和 id@0 用于同一数组中的未选中框。 我对 jquery 有点陌生,所以没有得到语法。请提出建议。

【问题讨论】:

  • 你能展示一下这个 JavaScript 生成的渲染 HTML 吗?并且,对于那个 HTML,显示你期望的精确输出。
  • 你好 Thomas,我的问题现在已经用 Suren ans 解决了。

标签: javascript jquery checkbox


【解决方案1】:

更新,这里是小提琴http://jsfiddle.net/MQQSv/1/

<table>
   <tr>
    <td>one</td>
    <td>
    <input type="checkbox" id='1'  checked/></td> 
   </tr>
   <tr>
    <td>two</td>
    <td>
    <input type="checkbox" id='2' /></td>
   </tr>
</table>

$('#save-btn').on('click', function() {
    var output = []
 $("table td input[type=checkbox]").each(function(){
   id = $(this).attr("id");
   output.push( id + "@" + ($(this).is(":checked") ? "1" : "0"))
  })
  console.log(JSON.stringify(output));
})

【讨论】:

  • 非常感谢。只是多一点帮助。现在数据以这种形式出现。 {"10020":false,"10021":true,"10022":true,"10023":false,"10024":false} 我需要这种形式的输出 id@1 为真,id@0 为假。
  • 非常感谢。我试过这个。但是您的代码行数较少。我把它弄大了。谢谢你。 $("#locationTable td input[type=checkbox]").each(function () { id = $(this).attr("id"); console.log(id); var eleIsselected = $(this). is(':checked') === true ? 1 : 0; output.push( id + "@" + eleIsselected ); })
  • @Maverick : 如果你觉得这个答案有用,别忘了接受这个答案。
【解决方案2】:

你可以试试这个: 将 id 推入两个不同的数组中

$(document).ready(function() {

    // bind click event to save-btn 
    $('#save-btn').on('click', function() {

        // init two array 
        // (if you want to use var out of on's callback function, you need to do declaration outside)    
        var checkedList = [],
            uncheckedList = [];

        // push ckecked id into checkedList
        $('input:checked').each(function() {
            checkedList.push($(this).attr('id'));
        });

        // push unckecked id into uncheckedList
        $('input').not(':checked').each(function() {
            uncheckedList.push($(this).attr('id'));
        });
    });
});

【讨论】:

  • 未选中的项目不起作用。它给了我语法错误。实际上我想遍历整个表,并将数据存储为 id@1 用于选中的框和 id@0 用于未选中的框。
  • 我编辑了答案 .not(':checked') 需要单引号
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
  • 2011-05-27
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
相关资源
最近更新 更多