2016-08-01 26 views
0

我很努力地让一段代码工作,但我不是一个jquery的人,所以请耐心等待。如何从选择器中删除元素

I have an outer DIV ($scope)。它包含各种输入。

我找到了每个输入类型的所有条目,并对它们进行过滤以获得具有值的条目。这些存储在$条目中。

$inputs contains all the inputs regardless of type or status. 

我想要做的是remove $entries from $inputs离开差异。

它不起作用,目前我没有收到任何反射的错误,所以没有什么可继续。

我的第一个想法是,jquery无法将一个列表中的元素与另一个列表中的元素进行匹配,因为它只保存了一个索引,而不是实际的对象。这可能是完全错误的(请参阅第1行)。无论哪种方式,我需要找到一种获取所有元素并将它们分成2位的方法 - 那些具有值和没有值的方法。

所有帮助表示赞赏。

function inputLoaded(isPostback) { 
      if (typeof Page_Validators !== "undefined") { 

$scope = $(".active-step:first"); 
       $inputs = $scope.find(inputs); 
      $cb = $scope.find(checkboxes).filter(":checked"); 
      $rb = $scope.find(radios).filter(":checked"); 
      $sb = $scope.find(selects).filter(function() { return $(this).val() !== "None"; }); 
      $ta = $scope.find(textareas).filter(function() { return $(this).val(); }); 
      $tb = $scope.find(textboxes).filter(function() { return $(this).val(); }); 
      $entries = $cb.add($rb).add($sb).add($ta).add($tb); 

      // Do things with $entries here 

      // Get elements that have not got entries 
      $el = $inputs.remove($entries); 

     } 
    } 
+0

'$ inputs.filter(input =>!$ entries.contains(input))'? – gcampbell

+0

@gcampbell - 当OP不使用它时为什么选择es6? – evolutionxbox

+0

'$ input.not($ entries)'会更简单。 –

回答

0

not()方法可以接受一个jQuery对象,其内容将被从您将其应用到jQuery对象中排除。它看起来完全像你要找的东西:

// Get elements, excluding entries. 
$el = $input.not($entries); 
0

我想你忘了写的选择:

function inputLoaded(isPostback) { 
    if (typeof Page_Validators !== "undefined") { 

     $scope = $(".active-step:first"); 
     $inputs = $scope.find("input, select, textarea"); 
     $cb = $scope.find(":checkbox").filter(":checked"); 
     $rb = $scope.find(":radio").filter(":checked"); 
     $sb = $scope.find("select").filter(function() { 
      return $(this).val() !== "None"; }); 
     $ta = $scope.find("textarea").filter(function() { 
      return $(this).val(); }); 
     $tb = $scope.find(":text").filter(function() { 
      return $(this).val(); }); 
     $entries = $cb.add($rb).add($sb).add($ta).add($tb); 

     // Do things with $entries here 

     // Get elements that have not got entries 
     $el = $inputs.remove($entries); 

    } 
}