首页 \ 问答 \ 通过传递复选框值属性jQuery来影响DOM元素(Affect DOM elements by passing checkbox value attribute jQuery)

通过传递复选框值属性jQuery来影响DOM元素(Affect DOM elements by passing checkbox value attribute jQuery)

我正在尝试通过选中复选框来更改div的属性值。 我试图通过将复选框值传递给函数来制作某种过滤器并过滤掉div 。 复选框是在id中使用id browse_options动态添加的。

<div subcategory='1'></div>
<div subcategory='2'></div>
<div subcategory='3'></div>

<input class="filter_checkbox" type="checkbox" value="1">
<input class="filter_checkbox" type="checkbox" value="2">
<input class="filter_checkbox" type="checkbox" value="3">

我的jQuery代码,基本上我试图循环输入然后div但是我现在不能让它工作几个小时所以我被困住了,任何帮助都会受到赞赏。

$('#browse_options').on("change", ".filter_checkbox", function(){
  $('.filter_checkbox').each(function (index, checkbox){
    if($(checkbox).prop('checked')){
      $('div#results>div.item.filter').each(function (index, card){
        if($(checkbox).attr('value') != $(card).attr('subcategory')){
          $(card).removeAttr('style');
          $(card).css('display','none');
        } else {
          $(card).removeAttr('style');
          $(card).addClass('filter2');
        }
      });
    }
  });
});

I'm trying to change attribute values of div by checking checkboxes. I'm trying to make some kind of filter and filter out div by passing checkbox value to the function. Checkboxes are added dynamically in div with id browse_options.

<div subcategory='1'></div>
<div subcategory='2'></div>
<div subcategory='3'></div>

<input class="filter_checkbox" type="checkbox" value="1">
<input class="filter_checkbox" type="checkbox" value="2">
<input class="filter_checkbox" type="checkbox" value="3">

My jQuery code, essentially I'm trying to loop through inputs and then div but I can't get it to work for few hours now so I'm stuck and any help would be appreciated.

$('#browse_options').on("change", ".filter_checkbox", function(){
  $('.filter_checkbox').each(function (index, checkbox){
    if($(checkbox).prop('checked')){
      $('div#results>div.item.filter').each(function (index, card){
        if($(checkbox).attr('value') != $(card).attr('subcategory')){
          $(card).removeAttr('style');
          $(card).css('display','none');
        } else {
          $(card).removeAttr('style');
          $(card).addClass('filter2');
        }
      });
    }
  });
});

原文:https://stackoverflow.com/questions/22809735
更新时间:2024-02-09 07:02

最满意答案

你正在使用的jQuery版本没有分配快捷方式$ 。 您可以将脚本方式引用的内容更改为分配快捷方式的内容,也可以在导入jQuery后立即更改

<script type="text/javascript">
$ = jQuery;
</script>

The version of jQuery you're using isn't assigning the shortcut $. You can either change what you're referencing to script wise to one that is assigning the shortcut, or, immediately after importing jQuery

<script type="text/javascript">
$ = jQuery;
</script>

相关问答

更多
  • jquery 1.6 chm 下载[2023-02-20]

    jquery 1.6 chm 至今都没有出现。 你的目的是 离线版本的 jquery 1.6 api,这个早就有人做出来啦。。。 jQuery 1.6 API 中文版 下载: jQuery 1.6 API 英文版 下载: jQuery 1.6 API 中文版[在线版]: jQuery 1.6 API 英文版[在线版]: http://zhidao.baidu.com/question/292018321.html
  • 1.5和1.6之间的主要差异 精简的访问控制层一键升级核心CMS J1.6到1.7是一个非常多的增量升级 - 对用户工作流程进行了很多细微的改进。 J1.8将被称为J2.5 - 但不要问为什么。 Main Differences between 1.5 and 1.6 Refined Access Control Layer One-click upgrades of core CMS J1.6 to 1.7 is very much an incremental upgrade - lots of sma ...
  • 我编辑了一个jsFiddles以处理新的1.6更改。 现在看起来很好: http : //jsfiddle.net/tomgrohl/RULJN/ 在您的marginLeft和marginTop我将marginLeft:-200的值更改为marginLeft:"-200px" 。 他们需要用引号来工作。 width和height相同。 为了使动画工作,我改变了以下内容: $('div').animate({ width:400, height: 400, marginLeft: -2 ...
  • 用Prestashop 1.7制作PrestaShop 1.6主题是不可能的,它永远不会。 1.7已经改变了很多模板结构和传递给模板的数据。 关键是要从模板中移除逻辑和计算,以便更轻松地创建主题。 这对团队来说是一个艰难的选择,但我们相信这是能够一次解决这么多问题的唯一方法。 朱利安 It's impossible to make a PrestaShop 1.6 theme work with Prestashop 1.7 and it will never be. 1.7 has changed a l ...
  • 你想避免浏览器检测,而是做功能detection.browser检测可以很容易伪造,因此是不可靠的。 John Resig在这个问题上发表了一篇不错的文章 。 有关更多详细信息,Google针对“ 浏览器检测与功能检测 ” You want to avoid browser detection & instead do feature detection.browser detection can be easily faked & therefore is unreliable. see a nice p ...
  • 我想也许你正在寻找的是dojo.place和dojo._toDom的组合(在= = 1.7时没有下划线)。 toDom函数接受字符串并将其转换为DOM元素或文档片段。 n = dojo._toDom("
  • foo
  • "); // n is a single DOM node n = dojo._toDom("foo"); // n is a DOM text node n = dojo._toDom("
  • foo
  • 查看完整的下载库(从1.8 - > 1.5)的谷歌代码 。 下载包括来源,演示,主题和测试 See google code for the full repository of downloads (from 1.8 -> 1.5). Downloads include source, demos, themes and tests
  • 使用了两者 - 在我看来,Prototype有更多用于在Javascript中使用各种类型的实用函数(它提供了许多字符串帮助器函数)。 jQuery的力量似乎在于它非常健壮的选择器引擎以及方法链。 此外,我发现jQuery的事件模型似乎对我更有意义,我也非常喜欢使用.data()api存储任意对象的事实。 最后,插件社区对于jQuery来说更加丰富,我通常会发现在那里有一个插件,它完全符合我的需求。 总之,我通常使用jQuery来处理所有JavaScript开发任务。 我发现Scriptaculous(建立 ...
  • 你正在使用的jQuery版本没有分配快捷方式$ 。 您可以将脚本方式引用的内容更改为分配快捷方式的内容,也可以在导入jQuery后立即更改 The version of jQuery you're using isn't assigning the shortcut $. You can either change what you're referencing to script wise to ...
  • 事件处理的工作方式在1.7版本中发生了变化。 在将添加到DOM之前,在它上面触发的事件不会像以前一样冒泡到 (在我看来,错误地认为1.7行为更有意义)。 在上触发事件可能有效,但由于事件没有冒泡到 ,因此无法调用处理.live()设置的实际处理程序。 编辑 - 它可能是事件冒泡的文档元素; 无论如何,重点是相同的。 再次编辑 - 这是一种使这项工作的方法,以便您可以在将元素添加到DOM之前触发处理程序: $('
    ').on('click', ...
  • 相关文章

    更多

    最新问答

    更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的