2013-08-21 188 views
4

我想獲得單選按鈕值並通過AJAX發送給PHP。ajax獲取單選按鈕值

我的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; 
}); 
<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> 
+2

對於初學者來說,你不能有多個元素相同的ID –

回答

11

首先你有很多重複的id的屬性,這是不正確。改爲使用類,然後使用:checked選擇器來獲取所選無線電的特定實例。您的其他radio輸入

<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 
var message_pri = $(".message_pri:checked").val(); 

等等:

試試這個。

1

不使用ID兩個時間第一件事

現在單選框的選中值使用

$("input:radio[name=post_pri] :selected").val(); 
0

試試這個使用serialize功能check serialize here

$("#save_privacy").submit(function(){ 
    var serialise = $("#save_privacy").serialize(); 
    $.ajax({ 
     url:'edit_check.php', 
     type:'POST', 
     data:serialise, 
     beforeSend: function(){ 
     $(".privacy_info") .html("<img src=\"style/img/ajax/load2.gif\" alt=\"Loading ....\" />"); 
     }, 
     success:function(data){ 
     $(".privacy_info") .html(data); 
     } 
    }); 
return false; 
});