【问题标题】:how to find first input/select in jquery如何在jquery中找到第一个输入/选择
【发布时间】:2016-09-22 22:12:42
【问题描述】:

I used the following line to do so, but it fails when a select comes on the form.它离开了选择,然后即使输入也停止工作。

var $ele = $("select,input[type='text'],input[type='password']:visible[required]").filter(function () 
    { return this.value.trim() == '' || this.value.trim().indexOf('--') != -1 ;
     }).eq(0);

【问题讨论】:

  • $('form').find('input[type=text]:first,select:first')$('form').find('input[type=text],select').first()

标签: jquery


【解决方案1】:

试试这个:

$('form').find('input[type=text],select').filter(':visible:first')

(Source)

【讨论】:

  • 我修改了我的函数,但还是不行,新函数:-- var $ele = $("select,input[type='text'],input[type='password']") .filter(":visible[required]").filter(function () { return this.value.trim() == '' || this.value.trim().indexOf('--') != - 1 || this.value.trim().indexOf('Select') != -1; }).eq(0);
  • 嗯,尝试将:visible[required] 替换为:visible:required:
【解决方案2】:

您可以寻找以下代码。

$('form').find('input[type=text],select').filter(':visible:first')

【讨论】:

  • 我可以多次使用过滤器,因为我需要检查一些条件,例如:- return this.value.trim() == '' || this.value.trim().indexOf('--') != -1
猜你喜欢
  • 2014-11-11
  • 1970-01-01
  • 2011-02-18
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
相关资源
最近更新 更多