问题
i have this problem with my website (MVC3, C#) and a barcode reader. In my form i have something like this:
<ajax form....>
<input type=text />
<div id=list>
</div>
</form>
And the input is filled with a barcode reader and automatically submits the form that with ajax, fills the div with the id = list. My problem is that with chrome and ie, after the text is submited, a downloads windows appears in chrome and the favorites window appears in ie. I suppose that is because the barcode reader insers a [CR][LF] in the text and that opens those windows. I thought that the barcode reader was inserting a ctrl-j in some moment because that combination opens the downloads window in chrome and the favorites in ie, but with firefox the downloads window doesnt open (and is also ctrl-j).
I dont want to tell my client to configure the barcode reader so my page works, so i want a sollution in javascript maybe that fixes that problem.
thanks!!!
回答1:
i found this link but the only solution was to change your scanner's default character... i'm not sure I can change mine though so , like you, I'd be looking for a browser-related fix too.. maybe javascript. i'll try handling the characters with javascript to prevent that from happening... if I have any success i'll try and remember to come back here and tell you hehehehehe
i think this solves the issue....
$(document).ready(function(){
$("#input").keydown(function(e){
if(e.which==17 || e.which==74){
e.preventDefault();
}else{
console.log(e.which);
}
})
});
lemme know whether his works for you too.. make sure you empty the cache too...
回答2:
this code works for me
$(document).ready(function(){
$("#input").keydown(function(e){
if(e.which==17 || e.which==74 || e.keyCode == 13){
e.preventDefault();
}
})
});
回答3:
My scanner (Intermec SR30) is set up to apply 3 new line characters after the bar code. I found this by opening Vim, insert, then scanned the bar code. Then I cat'ed the file to od -ax:
0000000 3 1 2 2 1 0 9 9 9 4 8 5 2 8 nl nl
3133 3232 3031 3939 3439 3538 3832 0a0a
0000020 nl
000a
0000021
I can trap the 'nl's with:
$(document).ready(function(){
$("#barcode").keypress(function(e){
console.log('"' + e.keyCode + '"\n');
if(e.keyCode == 13){
e.preventDefault();
}
})
});
but the download-window-open event (Ctrl+j from the keyboard) is slurped up by the browser before it gets to the open page. This problem also plagues Firefox 30.0.
回答4:
The code on the approved answer is blocking CTRL and J keys. This would block only CTRL + J
$("#barcode").keypress(function(event){
if(event.keyCode == 74 && event.ctrlKey){
event.preventDefault();
}
});
回答5:
This code below won't work. Because if barcode value has 'J' character in it, you won't be able to get the correct result. We try to add e.ctrlkey control but this time, only one character of the barcode will be retrieved. Solution via js seems to be hard. Maybe the best option is changing scanner settings.
$(document).ready(function(){
$("#input").keydown(function(e){
if(e.which==17 || e.which==74 || e.keyCode == 13){
e.preventDefault();
}
})
});
回答6:
This works for me.
<script>
document.addEventListener('keydown', function(event) {
if( event.keyCode == 13 || event.keyCode == 17 || event.keyCode == 74 )
event.preventDefault();
});
</script>
回答7:
in our angularjs application we solved this issue with this directive:
(function () {
"use strict";
angular
.module("Xeon.core")
.directive("xnOnBarcodeKeydown", xnOnBarcodeKeydown);
var whiteList = [67, 86, 65, 88];
/** @ngInject */
function xnOnBarcodeKeydown() {
return function (scope, element, attrs) {
element.bind("keydown",
function (event) {
if (event.ctrlKey && whiteList.indexOf(event.which) === -1) {
event.stopImmediatePropagation();
event.preventDefault();
event.stopPropagation();
return false;
}
if (event.which === 13) {
scope.$apply(function () {
scope.$eval(attrs.xnOnBarcodeKeydown, { 'event': event });
});
event.preventDefault();
}
});
};
}
})();
Angular version:
import { Directive, EventEmitter, HostListener, Output } from '@angular/core';
@Directive({ selector: '[appXnOnBarcodeKeydown]' })
export class XnOnBarcodeKeydownDirective{
@Output() onEnter = new EventEmitter();
private whiteList = [67, 86, 65, 88];
@HostListener('keydown', ['$event'])
keyDown(event) {
if (event.ctrlKey && this.whiteList.indexOf(event.which) === -1) {
event.stopImmediatePropagation();
event.preventDefault();
event.stopPropagation();
return false;
}
if (event.which === 13) {
this.onEnter.emit(event);
event.preventDefault();
}
}
}
来源:https://stackoverflow.com/questions/10353105/prevent-open-downloads-window-in-chrome-with-barcode-reader