简体   繁体   中英

asp.net gridview's checkboxes value

I have a gridview with checkbox and I'm trying to get whether the checkbox of the clicked row is checked or not.

<td>
<input type="button" id="upbutt" value="edit" />
</td><td>60</td><td>h767</td><td>itsatesthere</td><td>cat4</td><td><span class="aspNetDisabled" title="control"><input id="GridView1_ctl00_11" type="checkbox" name="GridView1$ctl13$ctl00" checked="checked" disabled="disabled" /></span></td>

 <td>
<input type="button" id="upbutt" value="edit" />
</td><td>55</td><td>F309</td><td>ahmeds</td><td>cat1</td><td><span class="aspNetDisabled" title="control"><input id="GridView1_ctl00_8" type="checkbox" name="GridView1$ctl10$ctl00" disabled="disabled" /></span></td>/tr>


    $(function () {

        var tr = $('#GridView1').find('tr');
        tr.bind('click', function (event) {
            var tds = $(this).find('td:not(:first-child)');


            $.each(tds, function (index, item) {
                if ('#txtbadd' + (index + 1) != '#txtbadd5') {
                    $('#txtbadd' + (index + 1)).val(item.innerHTML);
                    alert($("td:has(:checkbox:checked)").text());
                }
            });

            $("#modialog").dialog("open");
            return false;
        });
    });

alert($("td:has(:checkbox:checked)").text());

this code works but it bring all rows with selected checkbox. I just want to know whether the clicked row checkbox is checked or not.

You need to pass context of current row with your selector

Change

alert($("td:has(:checkbox:checked)").text());

To

alert($("td:has(:checkbox:checked)", this).text());

I have made little change to make two rows for demo which could help you understanding how it works and simplified javascript.

Live Demo

$('#GridView1 tr').bind('click', function (event) {
    alert($("td:has(:checkbox:checked)", this).length > 0 ? "Checked" : "Not checked");
});

As of jQuery 1.7, the .on() method is the preferred method over bind for attaching event handlers to a document. You can read more over here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM