I have a form where the inputs have names such as:
unit_price[1]
unit_price[2]
The only way I've found to access them from javascript is using:
document.getElementsByName("unit_price[1]")[0]
I was wondering开发者_JAVA百科 if there is a way to access them as a single array in one selector.
I'm looking for a pure javascript way to do this, but the page does have the YUI 2 framework loaded, in case there is a one step way of doing it using yui syntax.
From the YUI2 docs:
var nodes = YAHOO.util.Selector.query('input[name^=unit_price]');
Here's a native solution using querySelectorAll()
[docs] :
document.querySelectorAll("[name^=unit_price]");
Has pretty good browser support.
http://www.quirksmode.org/dom/w3c_core.html
Good idea to prefix the selector with input
like @davin did in his answer.
精彩评论