我的表单中有一个单选按钮,当我输入参考编号时,我可以使用 jQuery 获取相应的表单详细信息以及 source 值。我不知道如何将该值设置为单选按钮。谢谢!
<input type="radio" ng-model="selected" name="source" class="source" <?php echo ($row['source']=='VAT')?'checked':'' ?> value="VAT">VAT
<input type="radio" ng-model="selected" name="source" class="source" <?php echo ($row['source']=='CST')?'checked':'' ?> value="CST">CST
的jQuery
$('#gp_no').on('keypress', function(e){
if(e.keyCode === 13)
{
$(this).trigger("enterKey");
var val = $(this).val();
var dataString = "gp_no=" + val;
$.ajax({
type: "POST",
url: "post_process.php",
data: dataString,
dataType: 'json',
success: function(data){
$('.source').val(data.source);
}
});
}
});
post_process.php
if(ISSET($_POST['gp_no'])) {
$gp_no = $con->real_escape_string($_POST['gp_no']);
$query = "SELECT * FROM items WHERE items.gp_no = $gp_no";
if ($result = $con->query($query)) {
$row = $result->fetch_assoc();
$source = $row['source'];
}
$json = array($vehicle_no, 'source' => $source);
$json = json_encode($json);
echo $json;
}
答案 0 :(得分:1)
如果您希望获得所选单选按钮的值,可以执行以下操作:
$('input[name="source"]:checked').val()
当您收到来自ajax电话的回复时,请检查您的按钮:
首先改变这个:
<input type="radio" ng-model="selected" name="source" class="source" id="VAT" value="VAT">VAT
<input type="radio" ng-model="selected" name="source" class="source" id="CST" value="CST">CST
$("#"+data.source).prop("checked", true)
此假设您的返回值为VAT
或CST