通过ajax获取单选按钮值

Mah*_*amy 6 php ajax jquery

我想得到单选按钮值并通过AJAX发送给PHP.

我的AJAX正在运行但当前正在0每行中插入一个,所以它没有从单选按钮中获取值.任何帮助,将不胜感激.

$("#save_privacy").submit(function() {
  var message_pri = $("#message_pri").val();
  var follow_pri = $("#follow_pri").val();
  var post_pri = $("#post_pri").val();
  var check7 = $("#check7").val();

  var s = {
    "message_pri": message_pri,
    "follow_pri": follow_pri,
    "post_pri": post_pri,
    "check": check7
  }

  $.ajax({
    url: 'edit_check.php',
    type: 'POST',
    data: s,
    beforeSend: function() {
      $(".privacy_info").html("<img src=\"style/img/ajax/load2.gif\" alt=\"Loading ....\" />");
    },
    success: function(data) {
      $(".privacy_info").html(data);
    }
  });

  return false;
});
Run Code Online (Sandbox Code Playgroud)
<form id="save_privacy">
  <table align="center" width="70%" cellpadding="0" cellspacing="0" border="0">
    <tr>
      <td style="padding: 5px;">
        <b>Message Buttun: </b>
      </td>
      <td style="padding: 5px;">
        <input type="radio" id="message_pri" name="message_pri" value="1" /> ON
        <input type="radio" id="message_pri" name="message_pri" value="2" /> OFF
        <input type="radio" id="message_pri" name="message_pri" value="3" /> FOLLOWERS
      </td>
    </tr>
    <tr>
      <td style="padding: 5px;">
        <b>Follow Buttun: </b>
      </td>
      <td style="padding: 5px;">
        <input type="radio" id="follow_pri" name="follow_pri" value="1" /> ON
        <input type="radio" id="follow_pri" name="follow_pri" value="2" /> OFF
      </td>
    </tr>
    <tr>
      <td style="padding: 5px;">
        <b>Who Can Post On Your Profile: </b>
      </td>
      <td style="padding: 5px;">
        <input type="radio" id="post_pri" name="post_pri" value="1" /> Evry one
        <input type="radio" id="post_pri" name="post_pri" value="2" /> Followers
      </td>
    </tr>
    <tr>
      <td colspan="2" style="padding: 5px;">
        <input type="hidden" id="check7" value="save_privacy" name="check7" />
        <input class="small color blue button" type="submit" value="Save" />
      </td>
    </tr>
  </table>
  <div class="privacy_info"></div>
</form>
Run Code Online (Sandbox Code Playgroud)

Ror*_*san 18

首先,你有很多重复的id属性,这是不正确的.改为使用类,然后使用:checked选择器获取所选的无线电的特定实例.

试试这个:

<input type="radio" class="message_pri" name="message_pri" value="1" /> ON  
<input type="radio" class="message_pri" name="message_pri" value="2" /> OFF
<input type="radio" class="message_pri" name="message_pri" value="3" /> FOLLOWERS
Run Code Online (Sandbox Code Playgroud)
var message_pri = $(".message_pri:checked").val();
Run Code Online (Sandbox Code Playgroud)

等等你的其他radio输入.


Rit*_*tan 5

首先不要使用 id 两次

现在用于单选框使用的选定值

$("input:radio[name=post_pri] :selected").val();
Run Code Online (Sandbox Code Playgroud)