开发者

jQuery selectors: targeting inputs in a form

开发者 https://www.devze.com 2023-03-29 08:39 出处:网络
I have the following: $(\'#registration_form input[type=text], #registration_form input[type=password]\')

I have the following:

$('#registration_form input[type=text], #registration_form input[type=password]')

And I feel it can be much better: faster and shorter

Something like

$('#registration_form input[type=text,password]')

EDIT: THIS is what I really wante开发者_StackOverflow中文版d! Please take a look here


You can use find() with the :text and :password selectors to shorten your syntax:

$("#registration_form").find("input:text, input:password")

From a performance standpoint, it might be slightly faster, but not by much.

EDIT: Since :text and :password are not native CSS selectors, the following variation using filter() might be faster (and it's shorter in the first place):

$("#registration_form input").filter(":text, :password")


What you have isn't bad, if you really wanted "cleaner" syntax you could try this:

$("#registration_form").find("input[type=text], input[type=password]")

Slightly cleaner, but not necessarily faster. It has to find #registration_form just once now instead of twice, but you're adding a second method call in there. Also since you're already matching on an exact id, you're likely going to save very little processing time in my example above.


I'm afraid such selector doesn't exist. However you can use a class if you want to simplify your selector.

0

精彩评论

暂无评论...
验证码 换一张
取 消