使用类不工作的jquery过滤器

时间:2016-07-06 13:31:50

标签: jquery

我将禁用属性添加到具有类

的所有输入元素
.vehicleIsInInvoice

喜欢

$(".vehicleIsInInvoice :input").prop("disabled", true);

它的工作正确。 现在我必须在类

下排除输入元素
.excludeFromDisabled

因为我正在使用像

这样的jquery过滤器
$(".vehicleIsInInvoice :input").filter('.excludeFromDisabled').prop("disabled", true);

但这对我不起作用。 简而言之,我的课程就像

一样
.vehicleIsInInvoice -> .excludeFromDisabled -> input elements

页面的Html就像

<div class="vehicleIsInInvoice">
    <div class="details">
        <input type="text">
    </div>  
    <div class="excludeFromDisabled">
         <input type="text">
         <input type="text">
    </div>
</div>

4 个答案:

答案 0 :(得分:0)

filter() 方法过滤器输入元素不是其父元素,而是更新选择器。

&#xA;&#xA;
  $(“。vehicleIsInInvoice.excludeFromDisabled:input”)。prop(“disabled”,true);&#xA;  
&#xA;&#xA;


&#xA;或使用 filter() 方法

&#xA;&#xA;
  $('。vehicleIsInInvoice')。filter('。excludeFromDisabled')。find (':input')。prop(“disabled”,true);&#xA;  
&#xA;

答案 1 :(得分:0)

您可以先选择.excludeFromDisabled,而不是稍后再提交:

$(".vehicleIsInInvoice:not(.excludeFromDisabled) :input").attr("disabled", true);

答案 2 :(得分:0)

因为没有HTML源代码,我试图找出解决方案和上下文:

&#13;
&#13;
$(function () {
  $(".vehicleIsInInvoice :input").filter(function(e) {
    return $(this).closest('.excludeFromDisabled').length == 1;
  }).prop("disabled", true);
});
&#13;
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>

<form>
    <div class="vehicleIsInInvoice">
        <input value="vehicleIsInInvoice 1">
        <input value="vehicleIsInInvoice 2">
        <div class="excludeFromDisabled">
            <input value="excludeFromDisabled 1">
            <input value="excludeFromDisabled 2">
        </div>

    </div>
</form>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您的意思是(只有输入字段,有些是特定类):

&#13;
&#13;
$(".vehicleIsInInvoice")
  .filter(function(){
    return $(this).hasClass('excludeFromDisabled');
  })
  .css('background','yellow');
&#13;
div{width:100px;padding:5px;text-align:center;border:1px solid #ccc;}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<input type="text" class="vehicleIsInInvoice" /><br>
<input type="text" class="vehicleIsInInvoice" /><br>
<input type="text" class="vehicleIsInInvoice excludeFromDisabled" /><br>
<input type="text" class="vehicleIsInInvoice" /><br>
<input type="text" class="vehicleIsInInvoice excludeFromDisabled" /><br>
<input type="text" class="vehicleIsInInvoice" /><br>
<input type="text" class="vehicleIsInInvoice" /><br>
<input type="text" class="vehicleIsInInvoice excludeFromDisabled" /><br>
<input type="text" class="vehicleIsInInvoice" /><br>
&#13;
&#13;
&#13;

或者,你的意思是(在DIV中输入字段,某些DIV具有特定的类):

&#13;
&#13;
$(".vehicleIsInInvoice")
  .filter(function(){
    return $(this).hasClass('excludeFromDisabled');
  })
  .find('input').css('background','yellow');
&#13;
div{width:180px;padding:5px;text-align:center;border:1px solid #ccc;}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="vehicleIsInInvoice"><input type="text" /></div>
<div class="vehicleIsInInvoice"><input type="text" /></div>
<div class="vehicleIsInInvoice excludeFromDisabled"><input type="text" /></div>
<div class="vehicleIsInInvoice"><input type="text" /></div>
<div class="vehicleIsInInvoice excludeFromDisabled"><input type="text" /></div>
<div class="vehicleIsInInvoice excludeFromDisabled"><input type="text" /></div>
<div class="vehicleIsInInvoice"><input type="text" /></div>
<div class="vehicleIsInInvoice"><input type="text" /></div>
&#13;
&#13;
&#13;

或者, 你的意思是(在DIV中输入字段,某些输入具有特定的类):

&#13;
&#13;
$(".vehicleIsInInvoice")
  .filter(function(){
    return $(this).find('input').hasClass('excludeFromDisabled');
  })
  .find('input').css('background','yellow');
&#13;
div{width:180px;padding:5px;text-align:center;border:1px solid #ccc;}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="vehicleIsInInvoice"><input type="text" /></div>
<div class="vehicleIsInInvoice"><input type="text" class=" excludeFromDisabled" /></div>
<div class="vehicleIsInInvoice"><input type="text" class=" excludeFromDisabled" /></div>
<div class="vehicleIsInInvoice"><input type="text" /></div>
<div class="vehicleIsInInvoice"><input type="text" class=" excludeFromDisabled" /></div>
<div class="vehicleIsInInvoice"><input type="text" /></div>
<div class="vehicleIsInInvoice"><input type="text" class=" excludeFromDisabled" /></div>
<div class="vehicleIsInInvoice"><input type="text" /></div>
&#13;
&#13;
&#13;