首页 新闻 会员 周边

简单的JQuery中的each问题?

0
[已解决问题] 解决于 2012-04-26 09:55
  $(function () {
       var $div = $("#form1");
       $div.each(function (i, item) {
        alert(item);
         });
     });
 <form id="form1" runat="server">
    <input id="Text1" type="text" />
    <input id="Checkbox1" type="checkbox" />
    <input id="Text2" type="text"  />
    <input id="File1" type="file" />
    <input id="Button1" type="button" value="button" />
    </form>

each不是遍历每一个dom元素的,应该弹出5个消息框的,为什么就弹出了一个消息框?高手赐教。

学之乐的主页 学之乐 | 初学一级 | 园豆:33
提问于:2012-04-25 17:20
< >
分享
最佳答案
1
$(function () {
       var $div = $("#form1 input");
       $div.each(function (i, item) {
            alert(item);
        });
}); 
奖励园豆:5
johLife | 菜鸟二级 |园豆:232 | 2012-04-25 17:22
$("li").each(function(){
      alert($(this).text())
    });
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>

看这个例子弹出了三个li的值,是不是选择器中的元素必须是你所要遍历的元素。但是把上面的改成input有什么也弹不出。

学之乐 | 园豆:33 (初学一级) | 2012-04-25 17:57

@河洛之子: 我这里测试是可以弹出的。

johLife | 园豆:232 (菜鸟二级) | 2012-04-26 09:08
其他回答(1)
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>Untitled Page</title>
    <script type="text/javascript" src="js/jquery-1.6.4.min.js"></script>
    <script type="text/javascript">
        function de() {
            var $de = $("#form1 input");
            $de.each(function (i) {
                    alert(i);
            });
        }
        $(function () {
            de();
        });
    </script>
</head>
<body>
     <form id="form1" runat="server">
        <input id="Text1" type="text" />
        <input id="Checkbox1" type="checkbox" />
        <input id="Text2" type="text"  />
        <input id="File1" type="file" />
        <input id="Button1" type="button" value="button" />
    </form>
</body>
</html>
icepy | 园豆:587 (小虾三级) | 2012-04-25 19:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册