简体   繁体   中英

How can I pass the value and corresponding name of a checkbox when checked using javascript in ASP.NET?

I'm very new to ajax/javascript so I will try my best to explain my problem. Here's what I have so far:

$(function () {

        $("#chkFilter").on("click", "input", function (e) 
        {
            var filterCheckboxes = new Array();
            $("#chkFilter").find("input:checked").each(function () {
                //console.log($(this).val()); //works fine
                filterCheckboxes.push($(this).val());
                console.log($(this).val());

                //var filterCheckboxes = new Array();
                //for (var i = 0; i < e.length; i++) {
                //    if (e[i].checked)
                //        filterCheckboxes.push(e[i].value);
                //}
            });
        console.log("calling ajax");
        $.ajax({
            url: "/tools/oppy/Default.aspx",
            type: "post",
            dataType: "json",
            data: { UpdateQuery: filterCheckboxes }, // using the parameter name
            success: function (result) {
                if (result.success) {
                }
                else {
                }
            }
        });
        });
    });

Every time a checkbox is checked, ajax passes the data onto the server. Here is an example of some checkbox values after a few have been checked in the data form obtained from the Developer's Console:

您可以尝试以下代码:

filterCheckboxes.push($(this).prop("name") + "=" + $(this).val());

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