jquery选择器:ID以某些东西开头(关键字“section”)

时间:2011-08-12 18:52:59

标签: jquery css-selectors

我想定位所有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>

3 个答案:

答案 0 :(得分:41)

这可以作为选择器使用:

$('[id^=section] > [id^=pre]')

但是,我认为你不能改变输入元素的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>

$('[id^=section] > [id^=pre]').show()

答案 1 :(得分:12)

groups = $('[id^=section]');    

$.each(groups, function(key, group) {
    inputs = $(group).children('[id^=pre]'); 
});

答案 2 :(得分:12)

不是寻找部分ID,也许你应该在div中添加“section”和“pre”类?然后你可以搜索

groups = $('div.section div.pre');

您正在做的事实上是基于类的查找,这将是适当的机制。