用JQuery简单实现ajax

创建页面index.htm

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(function () {
$("#btnSubmit").bind("click", function () {
var username = $("#txtUserName").val();
var userpassword = $("#txtPassword").val();
if (username == "" || userpassword == "") {
$("#demo").html("<span style='color:red;'>对不起,用户名和密码不能为空!</span>");
return;
}
$.get("login.aspx", { username: username, userpassword: userpassword },
function (data) {
if (data == "true") {
$("#demo").html("<span style='color:red;'>恭喜您,登陆成功!</span>");
}
else {
$("#demo").html("<span style='color:red;'>对不起,用户名和密码错误!</span>");
}
});
});

}
);
</script>

</head>
<body>
<div id="demo">
</div>
<table style="">
<tr>
<td>
用户名:</td>
<td>
<input id="txtUserName" type="text" /></td>
</tr>
<tr>
<td>
密码:</td>
<td>
<input id="txtPassword" type="password" /></td>
</tr>
<tr>
<td>
</td>
<td>
<input id="btnSubmit" type="button" value="登陆" /></td>
</tr>
</table>
</body>
</html>





posted @ 2013-10-10 13:27  武二郎  阅读(123)  评论(0编辑  收藏  举报