如何通过jQuerypost()提交单选按钮值

How to submit radio button value via jQuery post()

本文关键字:提交 单选按钮 何通过 jQuerypost      更新时间:2023-09-26

我要做的是获取单选按钮值,并通过jQuery-post()将其提交到php文件中,然后获得响应。

当我尝试发送"城市"的值时,下面的代码运行良好。正如你所看到的,"测试"被注释掉了。"test"被注释掉后,程序就工作了,"city"的值被提交到php文件中,并被处理、返回和打印。

有人能发现获取单选按钮值的问题吗?

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#trans_type").click(function(){
    $('#realty_type').html("<b>Loading response...</b>");
    $.post("sql_s.php",
    {
      //  test: $('input[name=transaction_type]:checked', '#trans_type').val()
    city:"New York"
    },
    function(data,status){
    document.getElementById("object_type").innerHTML = data;
    });
  });
});
</script>
<meta charset="UTF-8">
</head>
<body>
<h1>Some title</h1><br />
<form action="" id="trans_type">
<b>What would you like?</b><br />
<input type="radio" name="transaction_type" value="2">Sell&nbsp;&nbsp;&nbsp;
<input type="radio" name="transaction_type" value="3">Buy
</form>
<div id="object_type">message comes here</div>

</body>
</html>

我建议更改

{
    test: $('input[name=transaction_type]:checked', '#trans_type').val(), 
    city:"New York"
},

替换为

 {
        test: $('input[name=transaction_type]:checked').val(), 
        city:"New York"
    },

无需放置当前ID并在您发布的值上添加逗号分隔符(,),以区分您的值。请参阅此JSFIDDLE

我认为代码中注释的那一行就是您想要的
只需添加一个逗号(,),因为它是一个对象,这是您需要的分隔符。。。

因此,使用:

{
    test: $('input[name=transaction_type]:checked', '#trans_type').val(), // <- comma here
    city:"New York"
},