<script src="jquery-1.4.2.js" type="text/javascript"></script>
<script src="jquery.popup.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#Text1").blur(function () {
var name = $("#Text1").val();
name = encodeURI(name)
$.post("product.ashx?name=" + name, function (data, status) {
if (status == "success") {
$("#Text2").val(data);
}
});
});
});
</script>
</head>
<body>
<input id="Text1" type="text" /><input id="Text2" type="text" />
</body>
-------------------------------------------------------------------------------
context.Response.ContentType = "text/plain";
string name = context.Request.QueryString["name"];
SqlConnection conn = new SqlConnection("server=.;database=student;uid=sa;pwd=123456");
conn.Open();
try
{
SqlCommand cmd = new SqlCommand("select * from product where proname='" + name + "'", conn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
context.Response.Write(dr["proprice"].ToString());
}
catch (Exception)
{
context.Response.Write("没有");
}
本文通过一个简单的示例展示了如何使用 jQuery 发送 AJAX 请求并从数据库中获取产品价格信息。该示例利用 jQuery 的 post 方法实现输入框中产品名称的模糊查询,并在另一个输入框中显示查询到的价格。

被折叠的 条评论
为什么被折叠?



