javascript - browser (chrome, firefox) jquery .val() does not work for autofilled input fields (username, password) - Stack Over

This code is executed to enable a login button only if the length isn't 0 and username and passwor

This code is executed to enable a login button only if the length isn't 0 and username and password isn't empty and so on. this works fine. But if i save my login credentials in my browser and i visit the website and see that the fields are prefilled the .val() functions seems not to return a value. if i hit any key the login button is enabled. So the function "Initialize state of login button" seems not to work. Are pre filled fields visible for jquery?

hope you understand me ^^ thank you!

    ready: function() {
        var view = this;
         //setup subViews
        view.setupSubs();
         //Initialize state of login button
        view.onKey();
    },
    "onKey": function(event) {
        var view = this,
            nickname = view.$('#nickname').val(),
            password = view.$('#password').val();

        if (!nickname || !password || nickname.length === 0 || password.length < 8) {
            view.$(':submit').attr('disabled', 'disabled');
        } else {
            view.$(':submit').removeAttr('disabled');
        }
    },

This code is executed to enable a login button only if the length isn't 0 and username and password isn't empty and so on. this works fine. But if i save my login credentials in my browser and i visit the website and see that the fields are prefilled the .val() functions seems not to return a value. if i hit any key the login button is enabled. So the function "Initialize state of login button" seems not to work. Are pre filled fields visible for jquery?

hope you understand me ^^ thank you!

    ready: function() {
        var view = this;
         //setup subViews
        view.setupSubs();
         //Initialize state of login button
        view.onKey();
    },
    "onKey": function(event) {
        var view = this,
            nickname = view.$('#nickname').val(),
            password = view.$('#password').val();

        if (!nickname || !password || nickname.length === 0 || password.length < 8) {
            view.$(':submit').attr('disabled', 'disabled');
        } else {
            view.$(':submit').removeAttr('disabled');
        }
    },
Share asked Jan 16, 2014 at 9:45 Matze IhnseinMatze Ihnsein 511 silver badge3 bronze badges 1
  • 1 This happens a lot with many forms if the fields are pleted with a cached version of information because the event is fired only once and js can't see the changes. – Geo Commented Jan 16, 2014 at 14:38
Add a ment  | 

2 Answers 2

Reset to default 6

N.B. this only covers Chrome.

I recently had a similar issue with a watermarked control. The "value" of autofilled password inputs in chrome appears blank to javascript. I'm assuming this is a security plug of some kind from Google. So you can't get the value in javascript but you can test if there is a value.

Chrome has a CSS selector refinement called -webkit-autofill. You can select on this using JQuery. Which gives us the test:

var hasHiddenValue = view.$('#password:-webkit-autofill').length > 0;

Well, it is possible that prefilling the fields in your websits happens via JavaScript. Then you cannot directly read this with a normal event because the document is cached once it is loaded and all changes are not inside this cache.

You should try to use a "on" function that is executed on a refresed version of your document.

Try:

$(document).on( "keyup", "#nickname, #password", function(event) {

});

Edit:

onKey does not exist, wether use: "keydown" or "keyup".

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745044917a4608036.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信