简体   繁体   English

有人输入时提示文本字段值

[英]alert text field value when someone enters

I have a text field say 我有一个文本字段说

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

When someone enters a value in this field, Is there a way to alert that value (without any button click) in javascript or jquery. 当有人在此字段中输入值时,是否可以通过javascript或jquery来提醒该值(无需单击任何按钮)。

Please help. 请帮忙。 Thanks 谢谢

On keypress you will get multiple alerts. 按下按键时,您会收到多个警报。 If you handle on focusout then you can see the typed value. 如果处理聚焦,则可以看到键入的值。 Use this code if you can handle on focusout. 如果可以处理聚焦,请使用此代码。

$('#hell').on('focusout', function(){
  alert(event.target.value);
})

You can use .keyup() event as shown below :- 您可以使用.keyup()事件,如下所示:-

$('#hell').on('keyup',function(){
   alert($(this).val());
});

Fiddle 小提琴

Or(if you want to alert text as soon as user finished typing then use .blur() event as shown) 或者(如果您想在用户输入完毕后立即提醒文本,请使用.blur()事件,如图所示)

$('#hell').on('blur',function(){
  alert($(this).val());   
});

Fiddle 小提琴

Or you can also use .focusout() event as shown 或者您也可以使用.focusout()事件,如图所示

$('#hell').on('focusout',function(){
    alert($(this).val()); 
});

Fiddle 小提琴

here are some following event for keyboard 这是键盘的一些后续事件

http://api.jquery.com/keypress/ http://api.jquery.com/keypress/

http://api.jquery.com/keydown/ http://api.jquery.com/keydown/

http://api.jquery.com/keyup/ http://api.jquery.com/keyup/

and try 并尝试

$('#hell').on('keyup',function() {
       alert($(this).val());
    });

or 要么

$('#hell').on('keydown',function() {
       alert($(this).val());
    });

or 要么

$('#hell').on('keypress',function() {
   alert($(this).val());
});

Just use focusout event, this way.. 这样就可以使用焦点事件。

$('#hell').on('focusout',function(){
   alert($(this).val());
});

Or you can use blur 或者您可以使用模糊

$('#hell').on('blur',function(){
  alert($(this).val())      
});

You can use keyup event like below 您可以使用如下所示的keyup事件

$('#hell').on('keyup',function()
{
   alert($(this).val());
});

Please Try this. 请尝试这个。

 <input id="hell" type="text" name="suraj" onChange="toggle()"/>

    <script language="JavaScript">
        function toggle() {
       var value = document.getElementById('hell').value;
       alert(value);
        }
    </script>

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

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