我对 jQuery 和 JSON 还很陌生,遇到了一个我无法解决的问题。我有一个文本框,我希望用户开始输入一个人的姓名,当他/她选择姓名时,它会使用他们的电子邮件地址自动填充另一个文本框。
我的代码如下。任何帮助使它工作将不胜感激。提前非常感谢!
标记
<input type="text" id="person_name" class="required" />
<input type="text" id="person_email_address" class="required email" />
jQuery
$( "input#person_name" ).autocomplete({
source: "/autoComplete.aspx",
dataType: "json",
minLength: 1,
select: function( event, ui ) {
//set email textbox to the email field of the selected item.
}
});
自动完成.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string term = Request.QueryString["term"];
if (!string.IsNullOrEmpty(term))
{
string sql = string.Format("select * from users where first_name like '%{0}%' or last_name like '%{0}%'", term);
DataTable dt = GetTable(sql);
foreach (DataRow dr in dt.Rows)
{
string name = dr["first_name"].ToString() + dr["last_name"].ToString();
string email = dr["email"].ToString();
}
Response.ContentType = "application/json";
Response.Write("what goes here so i can access both email and name in the select function?");
}
}