用jQuery动态填写表单值

用jQuery动态填写表单值,第1张

用jQuery动态填写表单值

假设此示例HTML:

<input type="text" name="email" id="email" /><input type="text" name="first_name" id="first_name" /><input type="text" name="last_name" id="last_name" />

您可以使用以下javascript:

$("#email").bind("change", function(e){  $.getJSON("http://yourwebsite.com/lokup.php?email=" + $("#email").val(),        function(data){          $.each(data, function(i,item){ if (item.field == "first_name") {   $("#first_name").val(item.value); } else if (item.field == "last_name") {   $("#last_name").val(item.value); }          });        });});

然后,您只有一个PHP脚本(在本例中为lookup.php),该脚本在查询字符串中包含一封电子邮件,并返回一个JSON格式的数组以及您要访问的值。这是实际访问数据库以查找值的部分:

<?php//look up the record based on email and get the firstname and lastname...//build the JSON array for return$json = array(array('field' => 'first_name',          'value' => $firstName),    array('field' => 'last_name',          'value' => $last_name));echo json_enpre($json );?>

您需要做其他事情,例如清理电子邮件输入等,但是应该使您朝着正确的方向前进。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5050241.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存