首页 新闻 赞助 找找看

样用Jq遍历一个DIV里面的所有input 并判断是否为空?

0
悬赏园豆:5 [已解决问题] 解决于 2012-10-31 11:22

请高手们解决。急急急急急~~样用Jq遍历一个DIV里面的所有input 并判断是否为空?

次时代的主页 次时代 | 初学一级 | 园豆:5
提问于:2012-10-31 10:36
< >
分享
最佳答案
0

非空判断是吧


这样:

<!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>
    <title></title>
    <script src="scripts/jquery-1.7.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnSubmit").click(function () {
                $("input[type='text']").each(function () {
                    if ($(this).val() == "") {
                        alert("不能为空!");
                    }
                })
            })
        })
    </script>
</head>
<body>
    <div id="test">
        <input type="text" id="txtName" />
        <input type="text" id="txtPwd" />
        <input type="button" id="btnSubmit" value="提 交" />
    </div>
</body>
</html>
收获园豆:3
oppoic | 小虾三级 |园豆:770 | 2012-10-31 10:55
<!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>
    <title></title>
    <script src="scripts/jquery-1.7.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnSubmit").click(function () {
                $("div").find("input[type='text']").each(function () {
                    if ($(this).val() == "") {
                        alert("不能为空!");
                    }
                })
            })
        })
    </script>
</head>
<body>
    <div id="test">
        <input type="text" id="txtName" />
        <input type="text" id="txtPwd" />
        <input type="button" id="btnSubmit" value="提 交" />
    </div>
</body>
</html>

确定$("div")容器不可缺少

sym_cn | 园豆:798 (小虾三级) | 2012-10-31 10:59
$("div").find("input[type='text']").each(function () {
                    if ($(this).val() == "") {
                        alert("不能为空!");
                    }
                })
sym_cn | 园豆:798 (小虾三级) | 2012-10-31 10:59

@sym_cn: 按我的代码的话 div可要可不要

按sym_cn的写法就必须得有div 你看他的jquery代码 是通过div向下找input的

楼主多试试就知道了,只是jq的最最基本的用法了

oppoic | 园豆:770 (小虾三级) | 2012-10-31 11:06

$(function () {
            $("#btninfo").click(function () {
                $("#test").find("input[type='text']").each(function(){
                    if ($(this).val() == "") {
                        {alert("带星号的不能为空!");return false;}
                    }
                })
            })
        })

 

综合两位的 解决~~~

次时代 | 园豆:5 (初学一级) | 2012-10-31 11:22
其他回答(1)
1
$("XXdiv").find("input[type='text']").each(function(){

if($(this).val()==""){
alert($(this).attr("id")+"空")

}

});
收获园豆:2
sym_cn | 园豆:798 (小虾三级) | 2012-10-31 10:41

不行呀

支持(0) 反对(0) 次时代 | 园豆:5 (初学一级) | 2012-10-31 10:52

@次时代: 你的Input  是 text吗?  还是其他的  

支持(0) 反对(0) sym_cn | 园豆:798 (小虾三级) | 2012-10-31 10:55

@sym_cn: 兄弟最近抢分很犀利嘛

支持(0) 反对(0) oppoic | 园豆:770 (小虾三级) | 2012-10-31 11:36

@心态要好: ?  

支持(0) 反对(0) sym_cn | 园豆:798 (小虾三级) | 2012-10-31 11:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册