onkeypress不工作,请指教

时间:2016-03-18 10:53:40

标签: javascript jquery onkeypress

以下代码似乎无法在IE中运行。在Chrome中,它仅在页面刷新几次后才起作用。有什么想法错了吗?感谢。

SCAN SHIPMENT:
<input type='text' name='scan_shipment' maxlength="30" id='scan_shipment'
maxlength="20" size='30' onkeypress="handleKeyPress(event,this.form)"/></td>



function handleKeyPress(e,form){
     alert("test");   
    var key=e.keyCode || e.which;
    if (key==13){

        var age = document.getElementById('scan_shipment').value;

        age = age.substring(1, age.length);
         var appropiateTr = document.getElementById(age);

    if(appropiateTr==null) {
         var $messageDiv = $('#message'); // get the reference of the div
    $messageDiv.show().html('<H1>Barcode not found in this Manifest</H1>');
   // show and set the message
    setTimeout(function(){ $messageDiv.hide().html('');}, 3000); // 3
    seconds later, hide

         var audioElement = document.createElement('audio');
        audioElement.setAttribute('src', 'error-beep.mp3');
        audioElement.setAttribute('autoplay', 'autoplay');
        audioElement.play();

    $('#scan_shipment').val("");
        return;
    } 

   ajaxFunction();

    }
    }

2 个答案:

答案 0 :(得分:0)

您可以关注此文档:DEMO1DEMO2

  

你应该使用事件hendler。所以你应该调用函数onkeypress   event()hendler。

答案 1 :(得分:0)

问题出在setTimeout(function(){ $messageDiv.hide().html('');}, 3000); // 3 seconds later, hide部分。应评论3000); // 3 seconds later, hide是什么。