最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery not getting the password field value - Stack Overflow

programmeradmin4浏览0评论

I have searched a lot in stackoverflow and other sites and got many solutions but nothing solved mine.That is why I am posting this.I have a password field in my web page, which was created dynamically on a link click.And I want to get the value typed in that password field into a variable when it loses focus.For that i created a jQuery function like this.

$('#parentdiv').on('focusout', '#passwordfield', function() {
      var pass1 = $('#passwordfield').val();
      alert(pass1);
      alert("Hello");
});

Here the first alert mand will displayed with nothing, but the second alert e with "Hello". Which means, when the password field loses its focus this function executes without any problem, but the problem is, it is not getting the password field value.

Please help me to solve this problem.

EDIT

This is my html

<input type="password" size=20 id="passwordfield" />

I have searched a lot in stackoverflow and other sites and got many solutions but nothing solved mine.That is why I am posting this.I have a password field in my web page, which was created dynamically on a link click.And I want to get the value typed in that password field into a variable when it loses focus.For that i created a jQuery function like this.

$('#parentdiv').on('focusout', '#passwordfield', function() {
      var pass1 = $('#passwordfield').val();
      alert(pass1);
      alert("Hello");
});

Here the first alert mand will displayed with nothing, but the second alert e with "Hello". Which means, when the password field loses its focus this function executes without any problem, but the problem is, it is not getting the password field value.

Please help me to solve this problem.

EDIT

This is my html

<input type="password" size=20 id="passwordfield" />
Share Improve this question edited May 9, 2013 at 4:15 user2356932 asked May 9, 2013 at 3:57 user2356932user2356932 971 gold badge1 silver badge6 bronze badges 18
  • 2 It wouldn't fix your problem, but since the event is delegated to #passwordfield, just use $(this).val() inside the handler instead of the way you currently are using .val() – Ian Commented May 9, 2013 at 4:00
  • 2 Works for me: jsfiddle/MDFY3 – David Tansey Commented May 9, 2013 at 4:01
  • 1 Also, it works fine for me with either way of getting .val() - jsfiddle/eD8va – Ian Commented May 9, 2013 at 4:01
  • Can you show your HTML? what browser are you running on? – Jaya Mayu Commented May 9, 2013 at 4:02
  • Try to reproduce your issue on jsfiddle – palaѕн Commented May 9, 2013 at 4:03
 |  Show 13 more ments

3 Answers 3

Reset to default 5

you can try this:

HTML code:

<form id='form'>
<input type='password' class='required' />
</form>

JavaScript:

$('#form .required').focusout(function(){
var txt = $(this).val();
$('.required').after(txt);
});

or you can check this code at http://jsfiddle/92y6b/

My solution based on above idea:

<input type="password" class="form-control" name="password" />
<script>
    $('input[name=password]').keyup(function(){
        var password = $(this).val();
    });
</script>

keyup/focusout may fail when typing very fast, using copy/paste etc.

You may try this:

var x = document.getElementById("myPsw").value;
发布评论

评论列表(0)

  1. 暂无评论