开发者

Submit data with ajax & jQuery

I need to submit some data via ajax. It is not a form, but there are couple of radiobuttons on the page that be开发者_运维技巧lond to a form.

I need to detect which of the two radio buttons is selected when I send ajax request, without submitting the form and append that value to my ajax post. How do I do that?

$.ajax({
    url: "ajax.php",
    type: "POST",
    data: "op="+act+"&radioButton="+id
});

<input class="radioB" type="radio" value="all" name="users" checked> All
<input class="radioB" type="radio" value="some" name="users"> Some


There are more options how to check which of the buttons is selected e.g.

$('.radioB:checked').val(); // returns the value or just val() without :checked
$('.radioB').is(':checked'); // can be used in a loop with if statement

So it would transform into:

$.ajax({
    url: "ajax.php",
    type: "POST",
    data: "op="+act+"&radioButton="+$('.radioB:checked').val()
});


var act = $(".radioB[name='users']").val();

will save all or some into act


This should give an idea of what to check for in regards to the getting radio button status (http://forum.jquery.com/topic/checking-if-certain-radiobutton-is-checked). As to passing that data on via jquery ajax, google that.


Just serialize and send the form data:

$.post('ajax.php', $('#myFormId').serialize());
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜