Check.ashx
<%@ WebHandler Language="C#" Class="Check" %>using System;
using System.Web;public class Check : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string a = context.Request.Form["name"];
string rs = string.Empty;
if (a == "1")
rs = "T";
else
rs = "F";
context.Response.Write(rs);
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}}Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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 runat="server">
<title>无标题页</title>
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
//验证名称
function checkName(){
var txt = $('#TextBox1');
var isok = true;
if($.trim(txt.val()) == ""){
isok = false;
}else{
$.post(
"Check.ashx",{name : txt.val()},function(rs){
if(rs == "F"){
$('#Label1').text('错');
isok = false;
}else{
$('#Label1').text('对');
}
}
);
}
if(isok){
alert(isok);
document.form1.submit();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return checkName()" />
</form>
</body>
</html>
为何isok一直是true!
如何修改?
<%@ WebHandler Language="C#" Class="Check" %>using System;
using System.Web;public class Check : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string a = context.Request.Form["name"];
string rs = string.Empty;
if (a == "1")
rs = "T";
else
rs = "F";
context.Response.Write(rs);
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}}Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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 runat="server">
<title>无标题页</title>
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
//验证名称
function checkName(){
var txt = $('#TextBox1');
var isok = true;
if($.trim(txt.val()) == ""){
isok = false;
}else{
$.post(
"Check.ashx",{name : txt.val()},function(rs){
if(rs == "F"){
$('#Label1').text('错');
isok = false;
}else{
$('#Label1').text('对');
}
}
);
}
if(isok){
alert(isok);
document.form1.submit();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return checkName()" />
</form>
</body>
</html>
为何isok一直是true!
如何修改?
var $txt = $('#TextBox1');
不是获取不是参数,name是可以获取到的。就是验证isok的true和false的时候,不正常!
function checkName(){
var isok = true;
if($.trim($('#TextBox1').val()) == ""){
isok = false;
}else{
$.post(
"Check.ashx",{name : $('#TextBox1').val()},function(rs){
if(rs == "F"){
$('#Label1').text('错');
isok = false;
}else{
$('#Label1').text('对');
}
}
);
}
return isok;
}
我把var txt = $('#TextBox1');去掉,全部换成$('#TextBox1')还是不行!
alert(isok);
document.form1.submit();
}
写到function(rs)内部看看
改
context.Response.ContentType = "text/html";
试下
$.ajax({
url: 'product.ashx?Param=getdetail',
data: 'productId=' + $(obj).attr('productId'),
dataType: 'json',//格式自定义
cache: false,
async: false,
success: function(jsonResult) {
//执行的代码
}
}
}
});楼主换成这个请求方法试试
注意这个属性,应该是执行顺序上的问题,改成同步的试试
function checkName(){
var isok = true;
if($.trim($('#TextBox1').val()) == ""){
isok = false;
}else{
$.ajax({
url: 'Check.ashx',
type : 'post',
data: 'name=' + $('#TextBox1').val(),
dataType: 'html',//格式自定义
async:false,
success: function(rs) {
if(rs == "F"){
$('#Label1').text('No');
isok = false;
}
}
}); }
return isok;
}
</script>
改成这样后,就正常了!为什么设置同步就可以了?async:false?