jQuery 清除输入[类型=密码]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6558157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery clear input[type=password]
提问by user398341
I'm trying to clear all the inputs that are password type when jquery validation fails - I tried the following (which doesn't work) :
当 jquery 验证失败时,我试图清除所有密码类型的输入 - 我尝试了以下操作(不起作用):
$('input[type=password]').val('');
Any ideas?
有任何想法吗?
回答by Darin Dimitrov
Try using the :password
selector as it will make your code shorter:
尝试使用:password
选择器,因为它会使您的代码更短:
$(':password').val('');
Also your initial code should work. Your problem is somewhere else. Unfortunately as you haven't stated in what context and how this is called or as you haven't explained what it doesn't workmean I cannot help you further.
您的初始代码也应该可以工作。你的问题在别处。不幸的是,由于您没有说明在什么上下文中以及如何调用它,或者您没有解释它不起作用的原因,这意味着我无法进一步帮助您。
回答by Felix
Try with
试试
$("input[type='password']").val('');
回答by Shahrokhian
short answer :
简短的回答:
to select all password fields using jquery you can use :
要使用 jquery 选择所有密码字段,您可以使用:
$(':password').val('');
but for better performancein modern browsers use
但为了在现代浏览器中获得更好的性能,请使用
$("input[type='password']").val('');
Detailed Answer
详细解答
:password Selector( from jQuery document )
:password 选择器(来自 jQuery 文档)
Description: Selects all elements of type password.
描述:选择密码类型的所有元素。
version added: 1.0
添加的版本:1.0
jQuery( ":password" )
jQuery(":密码")
$( ":password" ) is equivalent to $( "[type=password]" ).
$( ":password" ) 等价于 $( "[type=password]" )。
As with other pseudo-class selectors (those that begin with a ":") it is recommended to precede it with a tag name or some other selector; otherwise, the universal selector ( "*" ) is implied.
与其他伪类选择器(那些以“:”开头的选择器)一样,建议在它前面加上标签名或其他选择器;否则,将隐含通用选择器 ( "*" )。
In other words, the bare $( ":password" ) is equivalent to $( "*:password" ), so $( "input:password" ) should be used instead.
换句话说,裸 $( ":password" ) 等效于 $( "*:password" ),因此应该使用 $( "input:password" ) 代替。
Additional Notes:
补充说明:
Because :password is a jQuery extension and not part of the CSS specification, queries using :password cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use [type="password"] instead.
因为 :password 是 jQuery 扩展而不是 CSS 规范的一部分,所以使用 :password 的查询无法利用原生 DOM querySelectorAll() 方法提供的性能提升。为了在现代浏览器中获得更好的性能,请改用 [type="password"]。
回答by Madhu
I think you can try this:
我想你可以试试这个:
$("#id of a input field").val('');
回答by rafael
$('input[type^="password"]').each(function(){
// this is an example
});