我可以在通过“提交”按钮提交字段之前使用Ajax发布一些值吗?
我正在使用以下代码.它已成功完成,但我无法获取已发布的数据.
有什么线索吗?
您的帮助/评论将不胜感激.谢谢!
function auto_fill(){
var that = $(this),
url = './auto_fill.PHP',
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) {
document.getElementById("f_name").value="<?PHP echo $name ?>";
document.getElementById("phn_no").value="<?PHP echo $phn_num ?>";
}
});
return false;
}
解决方法:
尝试这个
function auto_fill(){
$.getJSON("./auto_fill.PHP?id_num=" + $("#id_num").val(),
function(data){
$.each(data, function(i,item){
if (item.field == "first_name") {
$("#f_name").val(item.value);
} else if (item.field == "phonenum") {
$("#phn_no").val(item.value);
}
});
});
}
auto_fill.PHP
$id = $_GET['id_num'];
//build the JSON array for return
$json = array(array('field' => 'first_name',
'value' => 'Your name'),
array('field' => 'phonenumber',
'value' => $phn),
array('field' => 'somethinglikediscription',
'value' => 'You entered ID as '.$id));
echo json_encode($json );
您可以通过此JSON数组将任何变量传递给此值.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。