★注意不能夠用webfrom裏面的控件javascript
效果圖:html
{第一種方法 沒有封裝}java
(一)創建一個webfrom窗口《Defauila.aspx》jquery
<title></title> <%-- <script src="Script/jquery-1.7.1.min.js"></script>--%> <script type="text/javascript"> function myXMLHttpRequest() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("sp").innerHTML = xmlhttp.responseText; } } var url = "../AJAX練習/xiangying.aspx?username=" + document.getElementById("t").value;//路徑,要寫的很是詳細 xmlhttp.open("GET", url, true); //除了GET方法還有一種POST (1)true 部分刷新
(2)falst 所有刷新 xmlhttp.send(); } </script> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td> 用戶名: </td> <td> <input onkeyup="myXMLHttpRequest()" id="t" type="text"> </td> <td> <span id="sp"></span> </td> </tr> <tr> <td> 密碼: </td> <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> </tr> </table> </div> </form> </body> </html>
(二 )再創建一個新的webfrom窗體《Defauil2.aspx》web
public partial class xiangying : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string userna = Request["username"].ToString(); if (userna=="abc") { Response.Write("√"); } else { Response.Write("×"); } } }
{第二種方法 已經封裝好的}ajax
(一)數據庫
<title></title> <script src="Js/jquery-1.11.2.js" type="text/javascript" ></script> <script> $(document).ready( function () { $("#txtUid").blur( function () { var txt = $(this).val(); //ajax發送文本信息出去 $.ajax({ url:"Default2.aspx",//接收請求的頁面 type: "POST",//請求發送方式 data: { uid: txt },//數據 datatype: "XML",//接收回送的信息格式設定 success: function (data) {//回調函數 var c = $(data).text(); alert(c); var name = $(data).find("#name").text(); alert(name); if (c == 0) { $("#Literal1").html("用戶名可用"); } else { $("#Literal1").html("用戶名不可用"); } } }); } ); } ); </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtUid" runat="server"></asp:TextBox> <div id="Literal1"></div> <br /> </div> </form> </body> </html>
(在添加一個顯示窗口)函數
public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string uid = Request["uid"].ToString(); string count = "0"; //根據uid查詢數據庫 if (uid == "張三") { count = "1"; } Response.Write("<?xml version='1.0'?>"); Response.Write("<count id='count'>"+count+"</count>"); Response.Write("<name id='name'>" + uid + "</name>"); Response.End(); } }