2

I am new to MVC 4 and making an internet application that uses a hand scanner to fill a text box. I was hoping to have it automatically activate a JavaScript function once the barcode has been scanned.

Scanning the barcode fills the active textbox with 12 characters, and I currently have to click a button after each scan. Is there a way to automatically activate the button or JavaScript function after the barcode is scanned, or after the 12th character is entered?

4

3 回答 3

2
$('#myTextBox').attr('maxlength','12');
$(document).on('change','#myTextbox',function(e) {
    if ($(this).val().length >= 12) {
        $('#myButton').click();
    }
});

或者,如果扫描程序例程支持键盘事件:

$('#myTextBox').attr('maxlength','12');
$(document).on('keyup','#myTextbox',function(e) {
    if ($(this).val().length >= 12) {
        $('#myButton').click();
    }
});

无论哪种方式,此解决方案都需要jQuery,一个 Javascript 库。

于 2013-07-18T14:26:34.757 回答
1

好吧,这在大多数情况下就足够了。

function functionThatActivatesWhenTwelveCharactersInForm(){

}

JS:

<input type="text" onkeyup="if(this.value.length>=12){functionThatActivatesWhenTwelveCharactersInForm()}; return true;">
于 2013-07-18T15:02:37.707 回答
1

您可以使用 setInterval 定期检查长度。就像是

var i=setInterval(function() {
   if (document.getElementById('myTextBox').value.length >=12) {
      clearInterval(i);

      // Execute your code here

   }
}, 100)

此代码每 100 毫秒检查一次文本框的长度,并在达到所需长度时 - 您可以执行代码

于 2013-07-18T14:30:40.203 回答