Jquery selector input[type=text]')

前端 未结 4 1153
不思量自难忘°
不思量自难忘° 2020-12-23 03:01

I wrote a code that basically selects all input type=text element like this:

$(\'.sys input[type=text]\').each(function () {}

4条回答
  •  渐次进展
    2020-12-23 03:28

    Using a normal css selector:

    $('.sys input[type=text], .sys select').each(function() {...})
    

    If you don't like the repetition:

    $('.sys').find('input[type=text],select').each(function() {...})
    

    Or more concisely, pass in the context argument:

    $('input[type=text],select', '.sys').each(function() {...})
    

    Note: Internally jQuery will convert the above to find() equivalent

    http://api.jquery.com/jQuery/

    Internally, selector context is implemented with the .find() method, so $('span', this) is equivalent to $(this).find('span').

    I personally find the first alternative to be the most readable :), your take though

提交回复
热议问题