我想做的是获取单选按钮值并通过 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
<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
关于javascript - 如何通过 jQuery post() 提交单选按钮值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26559938/