我有2个TextBox; tb1和tb2 .on加载,焦点在tb1 ..保存按钮tb1和tb2都是空的...我想只显示tb1错误信息......
这是我的代码
$('#btn_SaveCategory').click(function () {
var uname = $('#txt_CategoryName').val();
var Description = $('#txt_CategoryDesc').val();
if (uname == '') {
alert("Please Enter Category Name");
$('#txt_CategoryName').focus();
}
if (Description == '') {
alert("Please Enter Category Description");
}
});
我试图搜索但发现了一个相反的链接 :jQuery validate only validates one field
如果我错过了什么,请建议
答案 0 :(得分:0)
试试这个。
DEMO http://jsfiddle.net/7LRCs/
$('#btn_SaveCategory').click(function () {
var uname = $('#txt_CategoryName').val();
var Description = $('#txt_CategoryDesc').val();
if (uname == '') {
alert("Please Enter Category Name");
$('#txt_CategoryName').focus();
}
else if (Description == '') {
alert("Please Enter Category Description");
$('#txt_CategoryDesc').focus();
}
});
答案 1 :(得分:0)
您可以像这样使用if else if
$('#btn_SaveCategory').click(function () {
var uname = $('#txt_CategoryName').val();
var Description = $('#txt_CategoryDesc').val();
if (uname == '') {
alert("Please Enter Category Name");
$('#txt_CategoryName').focus();
}
else if (Description == '') {
alert("Please Enter Category Description");
$('#txt_CategoryDesc').focus();
}
});
或者您可以在检查下一个条件之前return
执行该功能。喜欢这个
$('#btn_SaveCategory').click(function () {
var uname = $('#txt_CategoryName').val();
var Description = $('#txt_CategoryDesc').val();
if (uname == '') {
alert("Please Enter Category Name");
$('#txt_CategoryName').focus();
return false;
}
if (Description == '') {
alert("Please Enter Category Description");
$('#txt_CategoryDesc').focus();
return false;
}
});