开发者

jQuery result to $.post

On a page, I have several checkboxes, the id is diffe开发者_JS百科rent for each. The number of checkbox is not fixed, may be one, 4, 10, ... I'd like get all the id and send them via a $.post.

How can I do this ?

Thanks,

Update 1:

var jqxhr = $.post("/Controller/MyAction", {
    field1: $("#field1").val(),
    benefic: $(":checkbox").serialize(),
    fileld2: $("#fileld2").val()
},
function (data) {
})
.success(function () {
})
.error(function (jqXHR, status, error) {
})
.complete(function () {

});

I don't see any value in my controller for "benefic" ... and it's the "id" of selected checkbox I need


Do this:

$.post('your_url', $(":checkbox").serialize() );

If your checkboxes have the same name, for example name="myCheckbox[]", you could do this:

$.post('your_url', $(":checkbox[name='myCheckbox[]']").serialize() );

Hope this helps.

EDIT: Solution for your Update 1:

$.post("/Controller/MyAction", {
    field1: $("#field1").val(),
    benefic: $(":checkbox:checked").attr("id"),
    fileld2: $("#fileld2").val()
},
...

This will do what you ask for.. but, are you sure it will always be only 1 checkbox. selected? If you need more than 1 (i'm almost sure you do), in which format do you need to send them? Ids separated by commas?

To send all checked ids, separated by commas, you would do this:

$.post("/Controller/MyAction", {
    field1: $("#field1").val(),
    benefic: $(':checkbox:checked').map(function() { return this.id; }).get().join(','),
    fileld2: $("#fileld2").val()
},
...

Cheers


I believe something like this would do the trick:

    var json_send = {};
    $("input:checkbox").each(function()
                             {
                                 //Push all the $(this).attr("id") into the Json

                             });
//And here you send them


You can use jQuery .serialize() function to get form fields values from your form and then send it through jQuery .post

For Example:

$.post("test.php", $("#testform").serialize());
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜