开发者

how to POST radio button values through jquery

开发者 https://www.devze.com 2022-12-17 17:27 出处:网络
i have this example code: while ($row = mysql_fetch_object($result1)) { echo \'<input type=\"radio\" name=\"vote\" value=\'.开发者_如何学Go$row->avalue.\'/>&nbsp;\';

i have this example code:

 while ($row = mysql_fetch_object($result1)) {                  
                    echo '<input type="radio" name="vote" value='.开发者_如何学Go$row->avalue.'/>&nbsp;';
                    echo '<label >'.$row->atitle.'</label><br>';
                }

this displays 4 radio buttons alongwith their labels. now I am using the following jquery function to POST.

$("#submit_js").click(function() {
    $.post(
    "user_submit.php", 
    {//how to POST data?}, 
    function(data){
    });
});

I want to post the value associated with the radio button. but how do i select the value? how do i determine what radio button is selected and POST it?


$("[name='vote']:checked").val() will get you the value of the selected radio button.

$("#submit_js").click(function() {
  $.post(
  "user_submit.php", 
  {vote: $("[name='vote']:checked").val()}, 
  function(data){
  });
});


Jquery serialize is the best way to do this kind of things: http://docs.jquery.com/Ajax/serialize

$("#submit_js").click(function() {
    $.post(
    "user_submit.php", 
    $("form").serialize(), 
    function(data){
    });
});


If there is no radio button selected the radio button will not be added to the serialized string. In this case we can make a workaround by adding another one exactly like the following:

<input type="radio" name="vote" value="" checked style="display:none;">
0

精彩评论

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