如何编写jQuery表达式,找到name属性值与属性值不同的输入id?
应该找到这个
<input name="foo" id="bar" />
Run Code Online (Sandbox Code Playgroud)
这不应该找到
<input name="foo" id="foo" />
Run Code Online (Sandbox Code Playgroud)
琐碎的事情就像$('input[name!=id]')失败一样,因为[]表达式需要在右侧保持不变.
RRK*_*RRK 18
您可以使用filter()函数执行此操作.并使用修剪来确保.
编辑简单版本
$('input').filter(function(){
return this.id != this.name;
});
Run Code Online (Sandbox Code Playgroud)
如果由于尾随或前导空格而出现任何问题,则可以使用以下内容.
$('input').filter(function(){
thisID = $.trim(this.id);
thisName = $.trim(this.name);
return thisID != thisName;
});
Run Code Online (Sandbox Code Playgroud)