admin管理员组

文章数量:1242840

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;

本文标签: javascriptjQuery not getting the password field valueStack Overflow