隐藏第一个收音机并使用jQuery标记



我有一组从外部服务器生成的单选选项。我需要隐藏任何带有"无"标签的收音机和标签。

以下是HTML的外观:

<div class="productAttributeList top-emx" style="">
<div class="productAttributeRow productAttributeConfigurablePickListSet">
<div class="productAttributeLabel">
    <label for="be91177516ac23bfe33ba180741486ee">
        <span class="name">Delivery Options:</span>
    </label>
</div>
<div class="productAttributeValue">
<div class="productOptionViewRadio">
<ul>
    <li>
        <label>
            <input type="radio" class="validation" name="attribute[139]"
            value="" checked="checked" />
            <span class="name">(None)</span>
        </label>
    </li>
    <li>
        <label>
            <input type="radio" class="validation"  name="attribute[139]"
            value="86" />
            <span class="name">Standard Shipping</span>
        </label>
    </li>
    <li>
        <label>
            <input type="radio" class="validation"  name="attribute[139]"
            value="89" />
            <span class="name">2 Day Express Shipping</span>
        </label>
    </li>
    <li>
        <label>
            <input type="radio" class="validation"  name="attribute[139]"
            value="90" />
            <span class="name">3 Day Express Shipping</span>
        </label>
    </li>
</ul>
</div>  
</div>

试过了,但我觉得我太离谱了,因为它什么都没用。

<script>  
    $('.productOptionViewRadio' li:first).hide();  
</script>

非常感谢。

如果包含(None)标签的li不总是.productOptionViewRadio列表中的第一个,则无法使用组合的选择器隐藏它(无论如何都可以忽略选择器中的轻微拼写错误)。

您可以使用:contains()选择器,它将匹配任何或所有<label>(None)</label>元素:

$(".productOptionViewRadio span.name:contains('(None)')").parents('li').remove();

这里有一个jsFiddle Demo,请注意它是如何删除列表后面的元素的(与其他答案不同,它只会删除第一个li,而不管它的<span>的内容如何)。

你非常接近。您的选择器出现错误

 $('.productOptionViewRadio li:first').hide();  

此外,您应该将代码包装在中

$( document ).ready(function() {
  // Handler for .ready() called.
     $('.productOptionViewRadio li:first').hide(); 
});

很抱歉,在看到未经编辑的问题时,我最初误解了您的要求,无论如何,您可以使用类.name选择跨度,然后使用条件its text equals to '(none)' filter这些集合,然后将其隐藏

$( document ).ready(function() {
   $('span.name').filter(function(){
          return $.trim(($(this).text()) === '(None)'
   }).parents('li').hide();
});

演示

最新更新