How to use POST button values ​​through jquery

I have this code example:

 while ($row = mysql_fetch_object($result1)) {                  
                    echo '<input type="radio" name="vote" value='.$row->avalue.'/>&nbsp;';
                    echo '<label >'.$row->atitle.'</label><br>';
                }

displays 4 switches along with their labels. I now use the following jquery function for POST.

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

I want to post the value associated with the switch. but how to choose a value? How to determine which radio button is selected, and POST is?

+3
source share
3 answers

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

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

If no radio button is selected, the switch will not be added to the serialized string. In this case, we can make a workaround by adding another one that is the same as the following:

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

Jquery - : http://docs.jquery.com/Ajax/serialize

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

Source: https://habr.com/ru/post/1729149/


All Articles