简体   繁体   English

在jQuery中使用.val()时输入字段不存在

[英]Input Field not coming when using .val() in jquery

I have a simple jquery and php program which is checking username availability. 我有一个简单的jquery和php程序,用于检查用户名的可用性。 But when i am running this code it is not showing the input field. 但是,当我运行此代码时,它没有显示输入字段。 Here is my jquery 这是我的jQuery

<html>
<head>
<title>the title</title>
   <script type="text/javascript" 
   src="js/jquery-1.4.1.min.js"></script>
   <script type="text/javascript" language="javascript">
$(document).ready(function() {

    $('#check').html(username);
    $.post( 
        "user_availability.php",
        { username: $("#username").val()},
        function(data) {
            $('#check').html(data);

        }
    );
});
   </script>
</head>
<body>

   <div id="check">

   </div><br><br>
<form>
 <input type="text" id="username" name="username" value="rajan" /></form>
</html>

Please help me out as i am a beginner to jquery. 由于我是jquery的初学者,请帮助我。

Define Username ,Using keyup method send the data and get the result 定义Username ,使用keyup方法发送数据并获取结果

Try this 尝试这个

$(document).ready(function () {
    var username = $("#username").val();
    $('#check').html(username);
    $('input:text').keyup(function () {
        $.post(
            "user_availability.php", {
            username: $(this).val()
        },
         function (data) {
            $('#check').html(data);

        });
    });
 $('#username').trigger('keyup');//if you want to trigger keyup onload add this
});
$(document).ready(function () {
    var username = $("#username").val();
    $('#check').html(username);
    $.post(
        "user_availability.php", {
            username: username
        },
        function (data) {
          $('#check').html(data);
        }
    );
});

username is 用户名是

not

defined 定义的

while trying to pass as an argument 试图通过作为争论

$('#check').html(username); // <---
          $.post( 
             "user_availability.php",
             { username: $("#username").val()},
             function(data) {
                $('#check').html(data);

      });
   });

and if you want to check everytime the input changes, you need to listen to it 如果您想每次输入更改时检查一下,就需要听一下

//keyup to check every time a character is entered
$('#username').keyup(function () {
        $.post(

// on change, to only check when user blurs the field
$(document).on("change","#username",function () {
        $.post(

you can try this 你可以试试这个

 <script>
$(document).ready(function() {
    $('#check').html($("#username").prop('value'));
      $.post( 
         "user_availability.php",
         { username: $("#username").val()},
         function(data) {
            $('#check').html(data);

  });

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM