我有以下问题:
我将数据从单选按钮提交到ajax请求,以便将数据移动到数据库中。
-html
<form class="siteInfo" action="ajax/site.php?nr=<?php echo $_GET['nr']; ?>" method="POST">
<input type="radio" name="transport_transport_id" value="1"> <span class="value">1</span><br/>
<input type="radio" name="transport_transport_id" value="2"> <span class="value">2</span><br/>
<input type="radio" name="transport_transport_id" value="3" tabindex="21"> <span class="value">3</span><br/>
- ajax发布的页面
foreach($_POST as $key => $val) {
if(!empty($val)) {
$result[$key] = $val;
//This should be passed to database update function
}
}
var_dump($_POST);
$site->setSiteFields($siteNumber, $result);
-ajax
$('.siteInfo').on('change', function() {
var that = $(this);
url = that.attr('action'),
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value){
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
$.ajax({
url: url,
type: type,
data: data,
success: function(response) {
console.log(response);
}
});
return false;
});
ajax返回它从拾取页面获得的响应,但无论我选择哪个单选按钮,我只返回最后一个值。任何人都可以告诉我出了什么问题?
提前致谢!
答案 0 :(得分:1)
试试这个
$('.siteInfo').on('change', function() {
var that = $(this);
url = that.attr('action'),
type = that.attr('method'),
data = {};
data[name] = that.find("input[name='transport_transport_id']:checked").val();
$.ajax({
url: url,
type: type,
data: data,
success: function(response) {
console.log(response);
}
});
return false;
});