【问题标题】:jQuery function filter with attribute name contains bracket is not working具有属性名称的jQuery函数过滤器包含括号不起作用
【发布时间】:2018-09-03 08:27:14
【问题描述】:

这是我的代码:

$(function() {
  console.log($('[name^=foo]').filter('[name=foo\\[front\\]]').length);
  console.log($('[name^=bar]').filter('[name=bar\\[front\\]]').length);
});
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" name="foo[front]" />
<input type="text" name="foo[front]" />
<input type="text" name="bar[front]" />

第一个 console.log 按预期返回 2,但第二个 console.log 返回 0,我应该是 1。我的过滤器选择器有什么问题?

【问题讨论】:

  • 在引号中传递属性值,那么你就不需要转义[]。使用.filter('[name="bar[front]"]').length);
  • @Satpal 根据link 元字符必须用两个反斜杠转义:\\ 所以我不认为我的代码不正确
  • 这是一个有效的错误..
  • 我不认为这是一个错误 jquery 中的选择器解析器不知道属性值中方括号的数量,它想知道属性值的开头。最好的方法是在选择器中的属性值周围添加引号。也就是说,您的代码可以是例如&lt;input name="]][[lololo[[["&gt;,这将是一个有效的代码。

标签: jquery filter


【解决方案1】:

已确认错误jQuery Bug Tracker #4170

【讨论】:

    猜你喜欢
    • 2017-08-11
    • 2018-09-18
    • 1970-01-01
    • 2012-03-21
    • 2011-10-07
    • 2012-02-06
    相关资源
    最近更新 更多