Jquery:遍历 li 并获取其中的元素

发布于 2024-10-01 19:20:15 字数 1062 浏览 0 评论 0原文

这个问题看起来很简单,但我就是做不好。

<ul id="myUL">
   <li id="item1">
      <select class="partDesc"><option>Front</option><option>Rear</option></select>
      <input type="text" class="itemDesc">
      <img src="images/myimg.jpg" class="itemImg" > 
   </li>
   <li id="item2">
      <select class="partDesc"><option>Front</option><option>Rear</option></select>
      <input type="text" class="itemDesc">
      <img src="images/myimg.jpg" class="itemImg" > 
   </li>
</ul>

这些

  • 项是使用 jquery 动态添加的。 我想遍历这些
  • 项并获取所有输入,包括来自 partDesc 的选定值、来自 itemDes的文本来自 itemImg 的 src
  • 这就是我坚持的地方:

    $("#myUL li").each(function() {<br>
        var partDesc = $(this).??;<br>
        var itemDesc = $(this).??;<br>
        var itemImg = $(this).??;<br>
     });
    

    感谢您的阅读。

    This question seems easy but I just can't get it right.

    <ul id="myUL">
       <li id="item1">
          <select class="partDesc"><option>Front</option><option>Rear</option></select>
          <input type="text" class="itemDesc">
          <img src="images/myimg.jpg" class="itemImg" > 
       </li>
       <li id="item2">
          <select class="partDesc"><option>Front</option><option>Rear</option></select>
          <input type="text" class="itemDesc">
          <img src="images/myimg.jpg" class="itemImg" > 
       </li>
    </ul>
    

    These <li> items are dynamically added using jquery.
    I want to traverse these <li> items and get all the inputs, including selected value from partDesc, text from itemDes and src from itemImg.

    Here is where I stuck:

    $("#myUL li").each(function() {<br>
        var partDesc = $(this).??;<br>
        var itemDesc = $(this).??;<br>
        var itemImg = $(this).??;<br>
     });
    

    Thank you for reading.

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    青柠芒果 2024-10-08 19:20:15

    下面的脚本应该对你有帮助。这是演示

    $("#myUL li").each(function() {
        var partDesc = $(this).find('.partDesc').val();
        var itemDesc = $(this).find('.itemDesc').val();
        var itemImg = $(this).find('.itemImg').attr('src');
        alert('partDesc: '+partDesc);
        alert('itemDesc: '+itemDesc );
        alert('itemImg: '+itemImg );
    });
    

    following script should help you. here is the demo

    $("#myUL li").each(function() {
        var partDesc = $(this).find('.partDesc').val();
        var itemDesc = $(this).find('.itemDesc').val();
        var itemImg = $(this).find('.itemImg').attr('src');
        alert('partDesc: '+partDesc);
        alert('itemDesc: '+itemDesc );
        alert('itemImg: '+itemImg );
    });
    
    梅倚清风 2024-10-08 19:20:15
    $("#myUL li").each(function() {
        var partDesc = $(this).children("select:selected").text();
        var itemDesc = $(this).children("input").val();
        var itemImg = $(this).children("img").attr("src");
    });
    
    $("#myUL li").each(function() {
        var partDesc = $(this).children("select:selected").text();
        var itemDesc = $(this).children("input").val();
        var itemImg = $(this).children("img").attr("src");
    });
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文