Jquery - 嵌套循环中的 $(this)

发布于 2024-08-29 16:30:00 字数 370 浏览 3 评论 0原文

我不知道如何在 Jquery 中做某事。假设我有一个包含许多选择下拉菜单的表单,并执行此操作...

$('#a_form select').each(function(index) {

});

在这个循环内时,我想循环每个选项,但我不知道如何执行此操作,是这样的吗? ...?

    $('#a_form select').each(function(index) {

        $(this + 'option').each(function(index) {
           //do things
        });
});

我无法完全让它工作,有建议吗?干杯。

I can't figure out how to do something in Jquery. Let's say I have a form with many select drop-downs and do this...

$('#a_form select').each(function(index) {

});

When inside this loop I want to loop over each of the options, but I can't figure out how to do this, is it something like this....?

    $('#a_form select').each(function(index) {

        $(this + 'option').each(function(index) {
           //do things
        });
});

I can't quite get it to work, and advice? Cheers.

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

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

发布评论

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

评论(3

Bonjour°[大白 2024-09-05 16:30:00

我相信你想写 $('option', this)
您还可以编写$(this).find('option')

I believe you want to write $('option', this).
You can also write $(this).find('option').

樱花落人离去 2024-09-05 16:30:00

我会尝试

$('#a_form select option').each(function(index) {
  //do those things
});

I would try

$('#a_form select option').each(function(index) {
  //do those things
});
平安喜乐 2024-09-05 16:30:00

您需要考虑 self 变量的范围

$('#a_form select').each(function(index) {

   var self = '';
   $(this).children('option').each(function(index) {
      self = this;
      
      //for example 
      height += $(self).outerHeight();
   });
   console.log(self);
   
   $(this).css('height', height + 'px');
});

You need to take into account the scope of the self variable

$('#a_form select').each(function(index) {

   var self = '';
   $(this).children('option').each(function(index) {
      self = this;
      
      //for example 
      height += $(self).outerHeight();
   });
   console.log(self);
   
   $(this).css('height', height + 'px');
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文