检测由条形码扫描仪(阅读器)javascript填充的文本

时间:2016-02-08 17:19:25

标签: javascript input

代码使用条形码扫描仪检查条形码。 insert_code由用户(键盘)填充,search_code由条形码扫描仪自动填充。 目前,如果两个输入都引入条形码扫描器值,代码就可以工作。这对我来说不起作用。

代码需要在以下时间运行:

    手动输入
  1. insert_code(键盘)和
  2. var search_code = document.getElementById('search_code'); var insert_code = document.getElementById('insert_code'); var result = document.getElementById('result'); var button = document.getElementById('button'); var audio = new Audio('sound.wav'); // respond to button click button.onclick = function validate(e) { e.preventDefault(); // show verification result: if (search_code.value == insert_code.value) { result.textContent = 'code ok'; result.className = "ok"; audio.play(); } else { result.textContent = 'code is not ok'; result.className = "not-ok"; } // clear input when wrong: if (search_code.value !== insert_code.value) { insert_code.value = ''; } return false; }; function clearField(input) { input.value = ""; };由条形码扫描器自动填充
  3. ....     
    <form>
            <input type="text" name="search_code" onfocus="clearField(this, this.placeholder='');" onblur="this.placeholder='introdu codul'" id="search_code" placeholder="introdu codul" autocomplete="off" value=""/><br/>
            <input type="" name="insert_code" onfocus="clearField(this, this.placeholder='');" onblur="this.placeholder='scaneaza codul'" id="insert_code" placeholder="scaneaza codul" autocomplete="off" value=""/><br/><br/>
            <input type="submit" id="button" name="button" value="verifica COD" />
        </form>
    
        </div>
      <div id="result"></div>
       </div>
       <script src="js/action_input.js"></script>
     </body>
    </html>
    {{1}}

    谢谢!

1 个答案:

答案 0 :(得分:0)

要阻止条形码阅读器填充textbox,只需停用onpaste事件。

$(document).ready(function(){
      $('#search_code').bind("cut copy paste",function(e) {
          e.preventDefault();
      });
    });