jQuery在输入文本上显示空字符串?

时间:2022-11-13 19:09:19

I have a form like this

我有这样的表格

<form action="">
  <label for="name">Name</label>
  <input type="text" id="name" />
</form>


<script type="text/javascript">
  jQuery(document).ready(function() {
    var Name = jQuery('#name').val();
    jQuery('#name').blur(function() {
      console.log(Name);
    });
  });
</script>

but here when I am entering any values in the input text field it is showing error like (an empty string). So can someone tell me what is the wrong here? How to solve this?

但是当我在输入文本字段中输入任何值时,它显示错误(如空字符串)。所以有人能告诉我这里有什么不对吗?怎么解决这个?

5 个解决方案

#1


3  

This line:

var Name = jQuery('#name').val();

is a one time operation, setting the value of Name to the value of the input when that code executes. It will not magically update the value of Name when the value of that element changes (why do so many people get the impression that it would?). You'll need to do this:

是一次性操作,将Name的值设置为该代码执行时的输入值。当该元素的值发生变化时,它不会神奇地更新Name的值(为什么这么多人会得到它的印象?)。你需要这样做:

jQuery(document).ready(function () {
    jQuery('#name').blur(function () {
        console.log(this.value);
    });
});

I've cut out the now pointless Name variable, and just used the element reference (this) directly to get its value property rather than using jQuery functions.

我已经删除了现在没有意义的Name变量,并且直接使用元素引用(this)来获取其value属性而不是使用jQuery函数。

#2


4  

<input type="text" id="name />

should be

<input type="text" id="name" />

working fiddle: http://jsfiddle.net/TdLH2/

工作小提琴:http://jsfiddle.net/TdLH2/

#3


2  

Name variable should be set inside onblur handler:

应该在onblur处理程序中设置名称变量:

jQuery('#name').blur(function () {
    var Name = jQuery(this).val(); //or this.value
    console.log(Name);
});

#4


0  

Try

<form action="">
  <label for="name">Name</label>
  <input type="text" id="name" />
</form>


<script type="text/javascript">
  jQuery(document).ready(function()
   {
    jQuery('#name').blur(function () {
    var Name = jQuery("#name").val();
    jQuery("#name").blur(function() 
     {
      console.log(Name);
    });
   });
  });
</script>

#5


0  

If you want to console.log whatever is typed into the box, you can do:

如果你想在console.log中输入任何内容,你可以这样做:

<script>
  jQuery(document).ready(function() {
    jQuery('#name').keyup(function() {
        var Name = jQuery('#name').val();
        console.log(Name);
    });
  });
</script>

At the moment, you're getting the value of #name on page load, which would be "", an empty string. As nothing has been entered.

目前,您在页面加载时获得#name的值,该值为“”,为空字符串。没有输入任何内容。

#1


3  

This line:

var Name = jQuery('#name').val();

is a one time operation, setting the value of Name to the value of the input when that code executes. It will not magically update the value of Name when the value of that element changes (why do so many people get the impression that it would?). You'll need to do this:

是一次性操作,将Name的值设置为该代码执行时的输入值。当该元素的值发生变化时,它不会神奇地更新Name的值(为什么这么多人会得到它的印象?)。你需要这样做:

jQuery(document).ready(function () {
    jQuery('#name').blur(function () {
        console.log(this.value);
    });
});

I've cut out the now pointless Name variable, and just used the element reference (this) directly to get its value property rather than using jQuery functions.

我已经删除了现在没有意义的Name变量,并且直接使用元素引用(this)来获取其value属性而不是使用jQuery函数。

#2


4  

<input type="text" id="name />

should be

<input type="text" id="name" />

working fiddle: http://jsfiddle.net/TdLH2/

工作小提琴:http://jsfiddle.net/TdLH2/

#3


2  

Name variable should be set inside onblur handler:

应该在onblur处理程序中设置名称变量:

jQuery('#name').blur(function () {
    var Name = jQuery(this).val(); //or this.value
    console.log(Name);
});

#4


0  

Try

<form action="">
  <label for="name">Name</label>
  <input type="text" id="name" />
</form>


<script type="text/javascript">
  jQuery(document).ready(function()
   {
    jQuery('#name').blur(function () {
    var Name = jQuery("#name").val();
    jQuery("#name").blur(function() 
     {
      console.log(Name);
    });
   });
  });
</script>

#5


0  

If you want to console.log whatever is typed into the box, you can do:

如果你想在console.log中输入任何内容,你可以这样做:

<script>
  jQuery(document).ready(function() {
    jQuery('#name').keyup(function() {
        var Name = jQuery('#name').val();
        console.log(Name);
    });
  });
</script>

At the moment, you're getting the value of #name on page load, which would be "", an empty string. As nothing has been entered.

目前,您在页面加载时获得#name的值,该值为“”,为空字符串。没有输入任何内容。