jquery 选择器:ID 以某物开头(关键字“section”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7044955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery selector: ID starts with something (keyword "section")
提问by mustapha george
I want to target all divs that have an id that begins with "section"; for each of these divs, I want to show child divs of type input that begin with "pre".
我想定位所有 id 以“section”开头的 div;对于这些 div 中的每一个,我想显示以“pre”开头的输入类型的子 div。
//groups = $('div [id^=section]');
groups = $('[id^=section]');
$.each(groups, function(key, group) {
alert(key + ': ' + group);
//inputs = group.('[id^=pre]');
});
<div id="section-A1">
<input id="preBeginDtl" name="BeginDtlFields" value="" type="hidden">
<input id="other" name="name2" value="" type="input">
<input id="preGroup" name="GRP" value="AA" type="hidden">
</div>
采纳答案by Jeremy Holovacs
Rather than look for a partial id, perhaps you should add classes "section" and "pre" to the divs? Then you can search on
与其寻找部分 id,也许您应该向 div 添加类“section”和“pre”?然后你可以搜索
groups = $('div.section div.pre');
What you are doing is essentially a class-based lookup, and that would be the appropriate mechanism.
您所做的本质上是基于类的查找,这将是适当的机制。
回答by markle976
This would work as the selector:
这将用作选择器:
$('[id^=section] > [id^=pre]')
But, I don't think you can change the type attribute for input elements. You would probably just want to set the 'pre' inputs as type="text" (instead of type="hidden"), and set their css display property to none. Then use the jQuery show() to unhide it.
但是,我认为您不能更改 input 元素的 type 属性。您可能只想将 'pre' 输入设置为 type="text"(而不是 type="hidden"),并将其 css display 属性设置为 none。然后使用 jQuery show() 取消隐藏它。
<div id="section-A1">
<input id="preBeginDtl" name="BeginDtlFields" value="" type="text" style="display:none">
<input id="other" name="name2" value="" type="text">
<input id="preGroup" name="GRP" value="AA" type="text" style="display:none">
</div>
and
和
$('[id^=section] > [id^=pre]').show()
回答by Nick Radford
groups = $('[id^=section]');
$.each(groups, function(key, group) {
inputs = $(group).children('[id^=pre]');
});