Py学习  »  Jquery

JS/jquery-将.filter的输出存储在数组或字符串中

Srj • 4 年前 • 278 次点击  

如何将filter()函数的输出存储在数组或字符串中,以便以后在另一个函数中使用?

jQuery('.marca').change(function(){
  var regex = new RegExp(/^2x\d$/);

  if(jQuery(this).find('input').prop("checked")) {

    jQuery(this).nextAll().filter(function() {

// I would like to save the output from this .filter 
// function to use to make a string of classes to pass to the next function.
      console.log(this.className.match(regex)); //output below
      return this.className.match(regex);
  })    
    .show();
  } else {
    jQuery(this).nextAll().hide();
  }
});

我使用上面的代码检查带有复选框的表单的类,并且仅当选中了上一个按钮时才显示“子类”。 我使用regex和filter()来查找和显示下一组类,并希望将生成的类传递到下一个jQuery选择器中,以避免手动添加它们。

https://jsfiddle.net/srjjj/brtp8x2h/9/

我试图简单地将结果添加到变量中,但它不会存储整个值集,而只存储最后一个值(.2x4)。

console.log output

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53022
 
278 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Davide Vitali
Reply   •   1 楼
Davide Vitali    5 年前

您是否尝试过在filter函数之外声明一个数组,然后在该数组中推送值?

var matched = [];
jQuery(this).nextAll().filter(function () {
    matched.push(yourFilteredElement);
});