newcode();
由外部输入(键盘或SNAPI)执行。结果,控制台日志显示barcode_id
的值为1234
。
但是,当单击/执行button_scan()
时,它总是告诉barcode_id
为空。
如何确保执行newcode()
时确实编辑了barcode_id
?
var barcode_id = '';
function button_scan() {
if(barcode_id!='') { // WHY????? always empty console shows: 1234
alert("DENY");
return false;
}
// ALLOW
}
function newcode() {
setTimeout(function() {
var new_barcode = $('#barcode').val();
if(new_barcode!='') {
barcode_id = new_barcode;
$('#barcode').val('');
console.log('>>> ', barcode_id); // prints value: 1234
}
}, 500);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value='' name='barcode' id='barcode' onkeydown="newcode();"/>
<button onclick="button_scan()">Button</button>
答案 0 :(得分:1)
if(barcode_id!='') //<---If the barcode_id is not an empty string
尝试:
if(barcode_id=='')//<---If the barcode_id IS an empty string