81,092
社区成员
发帖
与我相关
我的任务
分享
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>用户注册</title>
<script src="js/jquery-1.8.3.min.js"></script>
<script language="javascript" >
$(function(){
$("#test3").click(function(){
try{
$("#test1").blur();
$("#test2").blur();
$("#form")[0].submit();
}catch(e){
alert(e.message);
}
});
$("#test1").blur(function(){
if(!$(this).val())
throw new Error("test1 值错误");
});
$("#test2").blur(function(){
if(!$(this).val())
throw new Error("test2 值错误");
});
});
</script>
</head>
<body>
<form id="form" action="http://www.baidu.com">
userName:<input type="text" id="test1" > <br/>
password:<input type="text" id="test2" > <br/>
<input type="button" id="test3" value=" submit ">
</form>
</body>
</html>
换种方式 或许可以轻松解决。$("#Name").blur(function(){
var txt = $(this).val();
if(txt == ""){
f = false;
}else{
f = true;
}
return f;
});
这样应该也可以,就是把return语句放在判断外面$("#form的id").submit(function(){
if(a&&b&&c&&d&&e){
return true;
}else{
return false;
}
});
这样提交试试,#form的id改成你自己表单的id
$(function(){
var a = false;
var b = false;
var c = false;
var d = false;
var e = false;
var f = false;
$("#Til").blur(function(){
var txt = $(this).val();
if(txt == ""){
a = false;
return a;
}else{
a = true;
return a;
}
});
$("#Desc").blur(function(){
var txt = $(this).val();
if(txt == ""){
b = false;
return b;
}else{
b = true;
return b;
}
});
$("#SortName").blur(function(){
var txt = $(this).val();
if(txt == ""){
c = false;
return c;
}else{
c = true;
return c;
}
});
$("#City").blur(function(){
var txt = $(this).val();
if(txt == ""){
d = false;
return d;
}else{
d = true;
return d;
}
});
$("#Tel").blur(function(){
var txt = $(this).val();
if(txt == ""){
e = false;
return e;
}else{
e = true;
return e;
}
});
$("#Name").blur(function(){
var txt = $(this).val();
if(txt == ""){
f = false;
return f;
}else{
f = true;
return f;
}
});
$("#commentForm").validate({
rules:{
Til:{
required:true,
maxlength: 20,
minlength:6,
},
Desc:{
maxlength: 200,
},
SortName:{
required:true,
minlength:1,
},
City:{
required:true,
},
Tel:{
required:true,
maxlength: 13,
minlength:8,
},
Name:{
required:true,
}
},
messages: {
"Til":{
required:"不能为空",
minlength:jQuery.format("长度不要小于{0}"),
maxlength:jQuery.format("长度不要大于{0}")
},
"Desc":{
maxlength:jQuery.format("长度不大于{0}")
},
"SortName":{
required:"类别不能为空",
minlength:jQuery.format("长度不小于{0}")
},
"City":{
required:"城市不能为空"
},
"Tel":{
required:"不能为空",
minlength:jQuery.format("最少{0}位"),
maxlength:jQuery.format("最高{0}位")
},
"Name":{
required:"不能为空"
},
},
errorPlacement: function(error, element) {
if($(element).attr("name")=='Til'){
$("#span1").hide();
}
if($(element).attr("name")=='Desc'){
$("#span2").hide();
}
$(error).css("color","red");
error.appendTo(element.parent());
},
errorElement:"span",
});
$("#add").click(function(e){
var uName=$("#Name").val();
if(uName==""){
alert("您当前没有登录,请登录!!!");
return false;
}
else if(a&&b&&c&&d&&e&&f){
document.forms[0].submit();
}else{
return false;
}
});