jquery each中return问题

magicxin031 2009-05-24 06:49:07
我想这样:有几个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之后还是继续会执行下面语句。有没有什么解决方法,或者还有什么更好的方法。
...全文
1724 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
magicxin031 2009-05-25
  • 打赏
  • 举报
回复
感觉用catch来做还是满不错的,只要在catch里面加个return就够了,谢谢大家。
感觉jquery很好很强大!
monexus 2009-05-24
  • 打赏
  • 举报
回复
修正

function saveData(){
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();
}
}
cww2010 2009-05-24
  • 打赏
  • 举报
回复

<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>


monexus 2009-05-24
  • 打赏
  • 举报
回复
提供两种 都是用throw的

function saveData(){
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();
}
}
wz_307 2009-05-24
  • 打赏
  • 举报
回复
记住. $.each实际上也是jquery的自定义函数, 在循环体内return false表示跳出each函数, return true表示跳出这一次循环, 继续执行下一次循环. 所以要是想跳出整个函数, 只有用变量控制.

<!--
//-->
是防止不支持js的浏览器. 同意4L, 感觉现在都没用了..
monexus 2009-05-24
  • 打赏
  • 举报
回复
可以用
throw new Error('your message');
把return false;替换掉

在each外面
try{}catch(e){}
处理
mengxj85 2009-05-24
  • 打赏
  • 举报
回复
Up
magicxin031 2009-05-24
  • 打赏
  • 举报
回复
原来如此。
s_liangchao1s 2009-05-24
  • 打赏
  • 举报
回复
现在基本没什么用了 是为了避免不支持js的浏览器 现在浏览器都支持了
magicxin031 2009-05-24
  • 打赏
  • 举报
回复
...看来只能用变量控制了。。。谢啦。另问为什么<!-- //-->
把script包起来,有什么讲究吗。好像sytle也有用<!-- //-->
括起来的,有什么区别啊
s_liangchao1s 2009-05-24
  • 打赏
  • 举报
回复

<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>
magicxin031 2009-05-24
  • 打赏
  • 举报
回复
在线跪求啊,大大们来帮忙啊!

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧