简体   繁体   English

检测用户输入的文本框的值

[英]Detect the value of a Textbox entered by the User

I have a textbox where I can enter values directly from keyboard or from mouse click. 我有一个textbox ,可以在其中直接从键盘或鼠标单击输入值。 If I click from mouse the value is increment by one from the previous value. 如果单击鼠标,则该值将比上一个值增加一。 Whereas from keyboard I can enter any value. 而从键盘我可以输入任何值。 My question is how do I detect the value of the textbox entered by any user. 我的问题是如何检测任何用户输入的文本框的值。

suppose if any value is entered by keyboard I need to reset to zero. 假设如果键盘输入了任何值,我需要重置为零。

Here is what I got so far 这是我到目前为止所得到的

function AddTrackingItem()
{
    var counter;


$("#Item_Count").keyup(function (event) {
    if(event.which == 13)
        counter = 0;   // if value is enter from keyboard then reset value
    else
    {
        counter = $("#Item_Count").val();
    }
});

TIA TIA

I am assuming the html looks something like this: 我假设html看起来像这样:

<input type="text" id="item_count" value="0" />
<input type="button" id="btn" value="Increment"/>

You'll need an event handler for clicks on the button (obviously), and an event handler for the change event on the input field. 您将需要一个事件处理程序(用于单击按钮)(显然),并需要一个事件处理程序(用于在输入字段上更改事件)。 The click on the button will not trigger the change event, but changing the input field manually will. 单击按钮不会触发更改事件,但是将手动更改输入字段。 Therefore we can safely reset the counter to 0 if the user alters the field. 因此,如果用户更改该字段,我们可以安全地将计数器重置为0。

$('#btn').on( 'click', function() {
  $('#item_count').val( function( i, oldval ) {
    return (oldval*1) + 1;
  } );
} );

$('#item_count').on( 'change', function() {
  $('#item_count').val( '0' );
} );

Edit: There are only two ways of entering data. 编辑:只有两种输入数据的方法。 One is by keyboard. 一种是通过键盘。 The other one is by the button. 另一个是通过按钮。 That means that if the change event isn't triggered by the button, 100% of the cases where the change event is triggered, it must be via the keyboard. 这意味着,如果更改事件不是由按钮触发的,则在100%触发更改事件的情况下,必须通过键盘来触发。 You can alter the code a bit to include the .data(...) ( docs ) functionality of jQuery and do something like the following code. 您可以稍微修改一下代码以包括jQuery的.data(...)docs )功能,并执行类似以下代码的操作。 It will reset the input when it was altered, and subsequently the button was pressed. 更改输入后,它将重置输入,然后按下按钮。

$('#btn').on( 'click', function() {
  $('#item_count').val( function( i, oldval ) {
    if( $(this).data( 'fromKeyboard' ) == 1 ) {
      $(this).data('fromKeyboard', 0 );
      return 1;
    }
    return (oldval*1) + 1;
  } );
} );

$('#item_count').on( 'change', function() {
  $(this).data( 'fromKeyboard', 1 );
} );

Example on jsbin . jsbin上的示例

if you don't want user to manually enter value in the textbox, why not make it readonly? 如果您不希望用户在文本框中手动输入值,为什么不将其设置为只读? Anyway, if you want to do what you insist to do. 无论如何,如果您想做自己坚持要做的事情。 Here is how to do it. 这是怎么做。

$("#btn").click(function(){
    var currentVal = $("#item_count").val(); 
    var counter = parseInt(currentVal) + 1;
    $("#item_count").val(counter);
});

$("#item_count").keyup(function (event) {
    $("#item_count").val("0");
});

Notice that I don't have the listener for enter key event (13) because I change the value to 0 upon keyup. 注意,我没有Enter键事件(13)的侦听器,因为我在键入键时将值更改为0。 If you want to have the value change to 0 on enter key you can do what you have in your code. 如果要将Enter键上的值更改为0,则可以执行代码中的操作。 Remember to convert the string to int so you can add/increment the value. 请记住将字符串转换为int,以便您可以添加/增加值。 When you do a .val() it returns a string not int. 当您执行.val()时,它返回的字符串不是int。 Hope this helps. 希望这可以帮助。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。

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

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