简体   繁体   English

jquery过滤选择与过滤功能

[英]jquery filtering selections with the filter function

So according to this jQuery documentation "In order to get the best performance using :password, first select elements with a standard jQuery selector, then use .filter( ":password" ), or precede the pseudo-selector with a tag name or some other selector." 所以根据这个jQuery文档 “为了获得最佳性能使用:password,首先选择带有标准jQuery选择器的元素,然后使用.filter(”:password“),或者在伪选择器前加上标签名称或者一些其他选择器。“

So I comply and filter out the selection using the filter function but it returns 0; 所以我遵守并使用过滤函数过滤掉选择,但它返回0; no elements were selected. 没有选择任何元素。 Why is this? 为什么是这样?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jQuery</title>
<script src="jquery-1.9.1.js"></script>
<script>
$(document).ready(function() {

    var t = $("form").filter(":password");

    $("body").append(t.length);


});
</script>
</head>
<body>
<form action="" method="post">
<input type="password" name="file" />
</form>
</body>
</html>

You'll want to query <input> elements and filter on those: 您将要查询<input>元素并对这些元素进行过滤:

$('form input').filter(':password');

Right now you're selecting all <form> elements and filtering them for :password types (and there won't be any). 现在你正在选择所有<form>元素并过滤它们:password类型(并且不会有任何)。

Because you don't have any forms of type password. 因为您没有任何形式的密码。

You could use one of the following: $('form input').filter(":password") or $("form").find("input:password") 您可以使用以下之一: $('form input').filter(":password")$("form").find("input:password")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM