javascript jQuery 没有获取密码字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16454087/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:44:02  来源:igfitidea点击:

jQuery not getting the password field value

javascriptjquery

提问by user2356932

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.

我在 stackoverflow 和其他网站上搜索了很多,得到了很多解决方案,但没有解决我的问题。这就是我发布这个的原因。我的网页中有一个密码字段,它是在链接点击时动态创建的。我想当它失去焦点时,将在该密码字段中键入的值放入一个变量中。为此,我创建了一个像这样的 jQuery 函数。

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

Here the first alert command will displayed with nothing, but the second alert come 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.

这里第一个警报命令将不显示任何内容,但第二个警报带有“Hello”。这意味着,当密码字段失去焦点时,该函数执行没有任何问题,但问题是,它没有获取密码字段值。

Please help me to solve this problem.

请帮我解决这个问题。

EDIT

编辑

This is my html

这是我的 html

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

回答by Uchit Shrestha

you can try this:

你可以试试这个:

HTML code:

HTML代码:

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

JavaScript:

JavaScript:

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

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

或者您可以在http://jsfiddle.net/92y6b/检查此代码

回答by ?wie?u

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>