开发者

Getting value from input checkbox without form tag

开发者 https://www.devze.com 2023-02-18 18:57 出处:网络
I have pretty complicated listview component organized as table. On every tr line I have one td with <input type=\"checkbox\" value=\"number\">. All 开发者_StackOverflow中文版this is within for

I have pretty complicated listview component organized as table.

On every tr line I have one td with <input type="checkbox" value="number">. All 开发者_StackOverflow中文版this is within form runat="server" tag.

I have done check/uncheck all pretty much the same way the gmail has it. Table is reading 50 records for one page and if you check records and click <input type="button"> it will duplicate all selected records.

What I need to know is how to read values from checked checkboxes and pass them on as JSON/serializeArray.

Thank you.


if they are all in a form lets say with the class checkboxes:

to serialize all of the checked boxes:

var serial = $('form.checkboxes').serialize(); 

so to put that variable lets say in a post:

$.post('url', serial, function(data){ /*do something with data*/})


To simply get the value of a checkbox you could use something like the following:

$(document).ready(function () {
    $('#checkBoxId').change(function () {
        var isChecked = $('#checkBoxId').val();
        //proceed as needs be
    });
});

This will enable you to record that a change has taken place, and further store and/or act upon the value.

As for serialization and 'passing them on', we may need more data from you. <- scratch that! @maniator just fixed that for you.


Give the element an Id, such as:

<input id="checkBox1" type="checkbox" value="number" />

Then use JavaScript to check if it's checked:

var isChecked = document.getElementById("checkBox1").checked;

To get the value:

var theValue = document.getElementById("checkBox1").value;
0

精彩评论

暂无评论...
验证码 换一张
取 消