我想这样:有几个input框,设置class都是"frame1",我就是想用jquery的each循环来判断这些input元素是否都不为空。
HTML:
<tr>
<th valign="top"><span>*</span>联系地址</th>
<td valign="top" id="addressInfo"><input name="txdz" type="text" class="frame1" id="txdz" value="${company.txdz }"/></td>
</tr>
我是这样写的:
function saveData(){
$(".frame1").each(function(){
if(this.value==""){
var temp = $(this).parent().prev().text();
var name = temp.substring(1,temp.length);
alert(name+"不能为空!");
return false;
}
}
);
document.form.action="sss.do"; document.form.submit();
}
我的问题就是现在能判断不为空问题,但是return false之后还是继续会执行下面语句。有没有什么解决方法,或者还有什么更好的方法。
HTML:
<tr>
<th valign="top"><span>*</span>联系地址</th>
<td valign="top" id="addressInfo"><input name="txdz" type="text" class="frame1" id="txdz" value="${company.txdz }"/></td>
</tr>
我是这样写的:
function saveData(){
$(".frame1").each(function(){
if(this.value==""){
var temp = $(this).parent().prev().text();
var name = temp.substring(1,temp.length);
alert(name+"不能为空!");
return false;
}
}
);
document.form.action="sss.do"; document.form.submit();
}
我的问题就是现在能判断不为空问题,但是return false之后还是继续会执行下面语句。有没有什么解决方法,或者还有什么更好的方法。
<script type="text/javascript">
<!--function saveData(){
var oCount = 0;
$(".frame1").each(function(){
if(this.value==""){
var temp = $(this).parent().prev().text();
var name = temp.substring(1,temp.length);
oCount ++;
alert(name+"不能为空!");
}
}
)
if(oCount <= 0){
document.form.action="sss.do";
document.form.submit();
}
}
//-->
</script>
把script包起来,有什么讲究吗。好像sytle也有用<!-- //-->
括起来的,有什么区别啊
throw new Error('your message');
把return false;替换掉在each外面
try{}catch(e){}
处理
//-->
是防止不支持js的浏览器. 同意4L, 感觉现在都没用了..
try {
$(".frame1").each(function(){
if (this.value == "") {
var temp = $(this).parent().prev().text();
var name = temp.substring(1, temp.length);
throw name;
}
});
} catch (e) {
alert(e + "不能为空!");
}
document.form.action = "sss.do";
document.form.submit();
}function saveData(){
var errNames = [];
try {
$(".frame1").each(function(){
if (this.value == "") {
var temp = $(this).parent().prev().text();
var name = temp.substring(1, temp.length);
throw name;
}
});
} catch (e) {
errNames.push(e);
}
if (errNames.length) alert(e.join(',') + "不能为空!");
else {
document.form.action = "sss.do";
document.form.submit();
}
}
<script type="text/javascript">
<!--function saveData(){
var oCount = 0;
var name="";
$(".frame1").each(function(){
if(this.value==""){
var temp = $(this).parent().prev().text();
if(name=="")
name = temp.substring(1,temp.length);
oCount ++;
}
}
)
if(oCount > 0){
alert(name+"不能为空!");
}
else{
document.form.action="sss.do";
document.form.submit();
}
}
//-->
</script>
var errNames = [];
//try {
$(".frame1").each(function(){
if (this.value == "") {
var temp = $(this).parent().prev().text();
var name = temp.substring(1, temp.length);
errNames.push(e);
}
});
//} catch (e) {
//errNames.push(e);
//}
if (errNames.length) alert(e.join(',') + "不能为空!");
else {
document.form.action = "sss.do";
document.form.submit();
}
}
感觉jquery很好很强大!