最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to POST radio button values through jquery - Stack Overflow

programmeradmin3浏览0评论

i have this example code:

 while ($row = mysql_fetch_object($result1)) {                  
                    echo '<input type="radio" name="vote" value='.$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?

i have this example code:

 while ($row = mysql_fetch_object($result1)) {                  
                    echo '<input type="radio" name="vote" value='.$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?

Share Improve this question asked Jan 19, 2010 at 22:50 amitamit 10.3k23 gold badges76 silver badges125 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12

$("[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)

  1. 暂无评论