开发者

jQuery clear input[type=password]

开发者 https://www.devze.com 2023-03-17 08:11 出处:网络
I\'m trying to clear all the inputs that are password type when jquery validation fails - I tried the following (which doesn\'t work) :

I'm trying to clear all the inputs that are password type when jquery validation fails - I tried the following (which doesn't work) :

$('input[type=password]').val('');

Any 开发者_高级运维ideas?


Try with

$("input[type='password']").val('');


Try using the :password selector as it will make your code shorter:

$(':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 work mean I cannot help you further.


$('input[type^="password"]').each(function(){
    // this is an example
});


short answer :

to select all password fields using jquery you can use :

$(':password').val('');

but for better performance in modern browsers use

$("input[type='password']").val('');

Detailed Answer

:password Selector ( from jQuery document )

Description: Selects all elements of type password.

version added: 1.0

jQuery( ":password" )

$( ":password" ) is equivalent to $( "[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.

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.


I think you can try this:

$("#id of a input field").val('');
0

精彩评论

暂无评论...
验证码 换一张
取 消