HTML:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<asp:ScriptManager runat="server" ID="sm">
<Services>
<asp:ServiceReference Path="~/validateUserName.asmx" />
</Services>
</asp:ScriptManager>
<div>
<input type="text" id="userName" onchange="test();"/>
<span id="msg"></span>
</div>
<Services>
<asp:ServiceReference Path="~/validateUserName.asmx" />
</Services>
</asp:ScriptManager>
<div>
<input type="text" id="userName" onchange="test();"/>
<span id="msg"></span>
</div>
JS:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<script type="text/javascript">
function test() {
requestService = JQueryProj.validateUserName.GetUser(document.getElementById("userName").value, validate);
}
function validate(result) {
if (result == "false") {
msg.innerHTML = "用户名已存在!";
}
else {
msg.innerHTML = "用户名可用!";
}
}
</script>
function test() {
requestService = JQueryProj.validateUserName.GetUser(document.getElementById("userName").value, validate);
}
function validate(result) {
if (result == "false") {
msg.innerHTML = "用户名已存在!";
}
else {
msg.innerHTML = "用户名可用!";
}
}
</script>
Web Service
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
namespace JQueryProj
{
/// <summary>
/// validateUserName 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[ScriptService]
public class validateUserName : System.Web.Services.WebService
{
[WebMethod]
public string GetUser(string userName)
{
if (userName == "admin")
{
return false.ToString().ToLower();
}
else
{
return true.ToString().ToLower();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
namespace JQueryProj
{
/// <summary>
/// validateUserName 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[ScriptService]
public class validateUserName : System.Web.Services.WebService
{
[WebMethod]
public string GetUser(string userName)
{
if (userName == "admin")
{
return false.ToString().ToLower();
}
else
{
return true.ToString().ToLower();
}
}
}
}