jQuery:选择一个名称的多个选择标签 [英] JQuery: Select multiple select tags of a name
问题描述
我正在尝试访问具有特定名称的所有选择标签. 我尝试这样做:
I'm trying to access all my select tags with a specific name. I've tried doing this:
$("select[name='blah']")
和
$("select[name='blah[]']")
但都不起作用.
我遵循了JQuery文档,该文档显示了这样做是为了输入:
I followed the JQuery documentation that showed it doing it like this for inputs:
$("input[name='newsletter']")
我在做什么错了?
谢谢
推荐答案
Update, based on your comment: You should use an attribute-starts-with selector, like this:
$("select[name^='blah[']")
这将选择任何足以满足您需求的<select name="blah[.........">
.
This will select any <select name="blah[.........">
which should be specific enough for your needs.
此外,请确保您在这样的document.ready
上下文中运行选择器:
Also, be sure you're running your selector inside a document.ready
context like this:
$(function() {
$("select[name='blah']").doSomething();
});
如果不执行此操作,或者在页面中有问题的元素之后,该元素将无法选择...因此您的选择器可能还不错,但它所寻找的东西还不在DOM中.像上面一样,将您的代码放入document.ready
处理程序中可确保DOM具有所有可供选择的元素.
Without doing this, or after the element in question in the page, the element won't be there to select...so your selector may be just fine, but what it's looking for isn't in the DOM yet. Putting your code in a document.ready
handler like I have above ensures the DOM has all the elements ready to select.
这篇关于jQuery:选择一个名称的多个选择标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!