jQuery:从数组中设置复选框值 [英] jQuery: setting checkbox values from array

查看:59
本文介绍了jQuery:从数组中设置复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的视图中有一张桌子。这是由jquey绑定数据。我的表格中有一个复选框列。每行都有唯一的Id敌人。我在我的桌子上分页。现在我的问题是当我检查一些记录并转到另一页并在此检查一些记录。现在当我回到我的上一页复选框时没有选中。

我的代码是

Hi,
i have a table in my view. which is bind data by jquey. I have a checkbox column in my table. which have unique Id foe each row. i have paging in my table. now my problem is when i checked some record and go to another page and checked some record here. now when i back to my previous page check box not checked.
my code is

//for getting checked id
 function GetCheckedRecord() {
        var Ids = $('input[name=chkres]:checked').map(function () {
            return $(this).val();
        }).toArray();
        if (checkval.length > 0) {          
            checkval = checkval + ',' + Ids;
        }
        else {
            checkval = Ids;
        }
       // alert(checkval);
    }

// now click on prev button
 $(".prevPageBtn").on("click", function (e) {
            e.preventDefault();
            GetCheckedRecord();
 $('input[name=chkres]').map(function () {
                var a = ($.inArray($(this).val(), checkval) != -1);               
                $(this).prop("checked", a);
            });
            fetchData(page - 1);                      
        });

//  fetchData(page - 1); is a function which bind data in to table

function fetchData(whichPage)
    {
 var request = $.ajax({          
            url: '@Url.Action("GetJsonResult", "myController")',
            method: "POST",
            data: { "page": whichPage }
        });

        request.done(function (data) {
$.each(data, function (key, val) {
                var today = new Date();
                var bgClass = (key % 2 == 0) ? "search-result-bg" : "";
                var item = '<tr class="' + bgClass + '">';
                item += '<td width="15"><input type="checkbox" id=' + val["RecordID"] + ' value=' + val["RecordID"] + ' name="chkres" class="chkres" /> </td>';
                item += '<td >' + val["RecordID"] + '</td>';
 item += '</tr>';
                items.push(item);
            });
var result = '<div id="loadingOverlay"></div><tr class="search-result-heading">';
                result += '<td width="15">Select All<br/><input class=' + 'check_all' + ' type="checkbox" /></td>';
                result += '<td >RecordId</td>';
 result += '</tr>';
                result += items.join("");
                $("#dtsResult").html(result);
            } });} 
the problem is  <pre lang="c#">$('input[name=chkres]').map(function () {var a = $(this).val();}
show last pages id's of check box.

推荐答案

' input [name = chkres]:选中')。map(function(){
return
('input[name=chkres]:checked').map(function () { return


(< span class =code-keyword> this )。val();
})。toArray();
if (checkval.length > 0 ){
checkval = checkval + < span class =code-string>' ,' + Ids;
}
else {
checkval = Ids;
}
// alert(checkval);
}

// 现在点击上一步按钮
(this).val(); }).toArray(); if (checkval.length > 0) { checkval = checkval + ',' + Ids; } else { checkval = Ids; } // alert(checkval); } // now click on prev button


。prevPageBtn)。on( 点击,函数(e){
e.preventDefault();
GetCheckedRecord();
(".prevPageBtn").on("click", function (e) { e.preventDefault(); GetCheckedRecord();


这篇关于jQuery:从数组中设置复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆