Find first empty input field

前端 未结 4 2052
温柔的废话
温柔的废话 2021-01-05 08:33

This simple selector finds the first empty text input field in my form but skips over password type inputs:

$(\'input:text[value=\"\"]:first\').focus();


        
相关标签:
4条回答
  • 2021-01-05 09:17

    By using comma you can insert multiple selector. It works like an OR operator.

    $('input:text[value=""]:first,input:password[value=""]:first')
    
    0 讨论(0)
  • 2021-01-05 09:21
    $('input:text[value=""]').add('input:password[value=""]').first().focus();
    

    I don't think there is a better way. That's what I thought before taking a look at @Hari Das answer which has an issue but brings alternative solution to my mind:

    $('input:text[value=""],input:password[value=""]').first().focus()
    

    Choose the one which is more readable to you.

    0 讨论(0)
  • 2021-01-05 09:26

    Here a little custom expression :

    $.expr[':'].is = function(a,b,c){
        return $(a).is(c[3])
    }
    

    That allow you to use is in a selector. You can achieve what you want with that :

    $('input[value=""]:is([type=text],[type=password]):first')
    

    Fiddle

    0 讨论(0)
  • 2021-01-05 09:28

    The visible is there to skip any hidden fields, such as select2 autogenerated inputs.

    $('input:text[value=""]:visible:first',input:password[value=""]:first)
    
    0 讨论(0)
提交回复
热议问题