在jQuery中是否有内置的函数/选择器来按值过滤输入?
我可以创建一个自定义选择器:
$.extend($.expr[":"], {
val: function(elem, index, match) {
return $(elem).val() == match[3];
}
});
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:$("input:val('Some text')").
我只是想知道这样的事情是否已经存在或者是否有更好的方法.
它看起来不像是内置的解决方案.但对于一次性选择器,.filter()是一个选项:
$('input').filter(function() { return $(this).val() === 'Some text'; });
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
$('input[value=someval]')
Run Code Online (Sandbox Code Playgroud)
你也可以链接它
$('input[type=radio][value=someval]')
Run Code Online (Sandbox Code Playgroud)
看这里: