对于前端开发,我非常绿色,我尝试使用Modal插入数据,我有两个文本框,第一个是代码,第二个是名称.. 我需要它们是独一无二的:
$.validator.addMethod("codeNotUsed", function (value, element) {
var response;
$.ajax({
type: "POST",
url: "Profile_ValidatieProfileCode.ashx",
data: { code: $('#txtCode').val() },
async: false,
success: function (data) {
response = eval(data);
}
});
alert(response);
return response;
}, "Code already used");
$.validator.addMethod("nameNotUsed", function (value, element) {
var response;
$.ajax({
type: "POST",
url: "Profile_ValidateProfileName.ashx",
data: { name: $('#txtName').val() },
async: false,
success: function (data) {
response = eval(data);
}
});
return response;
}, "Name already used");
其中一个.ashx文件的内容:
Imports System.Web
Imports System.Web.Services
Public Class Profile_ValidateProfileName
Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/plain"
Dim name = context.Request.Form("name")
Try
Dim type = DataFactory.Instance.GetProfileByName(UserIdentity.ClientConfig.ConnectionString, name)
If IsNothing(type) Then
context.Response.Write("true")
Else
context.Response.Write("false")
End If
Catch ex As Exception
context.Response.Write("false")
End Try
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
结束班
HTML代码:
<form id="InputForm" name="InputForm" action="javascript:void(0);" novalidate="novalidate"><div id="InputFormContent">
<br>
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<label>Code</label>
<div class="icon-addon">
<input id="txtCode" name="txtCode" class="form-control required" maxlength="10">
<i class="fa fa-edit"></i>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<label>Name</label>
<div class="icon-addon">
<input id="txtName" name="txtName" class="form-control required" maxlength="30">
<i class="fa fa-edit"></i>
</div>
</div>
</div>
</div>
</div>
独特的验证不起作用,而且我认为ajax代码运行不正常,警报给了我&#39; Undefined&#39; ..
答案 0 :(得分:0)
您的调用ajax无效,因为您必须先调用ajax并在完成后检查结果并调用&amp; .validator.addMethod
$.ajax({
type: "POST",
url: "Profile_ValidatieProfileCode.ashx",
data: { code: $('#txtCode').val() }
}).done(function (data) {
var response = eval(data);
alert(response);
$.validator.addMethod("codeNotUsed",function (value, element) { return response;}, "Code already used");
});
$.ajax({
type: "POST",
url: "Profile_ValidateProfileName.ashx",
data: { name: $('#txtName').val() }
}).done(function (data) {
var response = eval(data);
alert(response);
$.validator.addMethod("nameNotUsed",function (value, element) { return response;}, "Name already used");
});
答案 1 :(得分:0)
在成功函数被调用之前执行了alert语句,在完成ajax请求后执行alert语句在这种情况下工作得更好: 守则:
$.validator.addMethod("codeNotUsed", function (value, element) {
var response;
$.ajax({
type: "POST",
url: "Profile_ValidatieProfileCode.ashx",
data: { code: $('#txtCode').val() },
async: false,
error: function (xhr, status, error) {
alert(error)
}
}).done(function (data) {
response = eval(data);
alert(response); // this will give true or false accourding to server response
});
return response;
}, "Code already used");
$.validator.addMethod("nameNotUsed", function (value, element) {
var response;
$.ajax({
type: "POST",
url: "Profile_ValidateProfileName.ashx",
data: { name: $('#txtName').val() },
async: false,
error: function (xhr, status, error) {
alert(error)
}
}).done(function (data) {
response = eval(data);
alert(response); // this will give true or false accourding to server response
});
return response;
}, "Name already used");