简体   繁体   English

按键事件在magento中不起作用

[英]keypress event is not working in magento

I'm having problems with keypress events.first i am enter the key then it's working but next time enter the key is not working.Please tell me what's going wrong with my code: 我在按键事件方面遇到问题。首先我输入密钥然后它可以工作,但是下次输入密钥不起作用。请告诉我我的代码出了什么问题:

html code: html代码:

      <div class="findbysku">
                <input type="text" class="input-text" name="q" id="finder" title="Model #" onblur="if (this.value == '') { this.value = this.defaultValue;}" onfocus="if (this.value == this.defaultValue) {this.value = '';}" value="Model #" on/> 
                <button class="button" title="Search" id="finder_find" type="submit">Search</button>
            </div>

javascript code: JavaScript代码:

type="text/javascript"></script>
<script type="text/javascript" >

     $.noConflict(); 
    jQuery(document).ready(function(){

    var options, a;
    jQuery(function(){
        options = { serviceUrl:'<?php echo $url; ?>finder/index/loadproductsbyquery', 
                    onSelect: function(value, data){ open_product(data);  },
                    maxHeight:400,
                    minChars:3,
                    width:300,
                    noCache: false
                  };
        a = jQuery('#finder').autocomplete(options);
    });          

    jQuery('#finder').keypress(function(event) {

        if (event.keyCode == 13) {

           if (a.data.length > 0 ) {

               open_product(a.data[0]);

           }
        }
    });

    jQuery('#finder_find').click(function() {  

           if (a.data.length > 0 ) {    

               open_product(a.data[0]);
           }        

    });

    jQuery('.list-categories select').change(function() {
        window.open(jQuery(this).val(),'_self');    
    }); 
});

jQuery("#sample-review-click").fancybox({
    padding : 0  
});

jQuery("#guidelines-click").fancybox({
    autoSize    : true,
    maxWidth    : 600,

}); 
function open_product(data) {

    jQuery('.product-container').css('background-color','#FFFFFF');
    jQuery.post('<?php echo $url; ?>finder/index/getproductinfo',{ id: data} , function(response) {
        var response_array=JSON.parse( response );                   
        var url=response_array.url+"#box-Reviews";
        window.location.href = url;
    });             
}   

</script>

keypress event is working fine.Probably some other issue keypress事件运行正常。可能还有其他问题

  $.noConflict(); 
jQuery(document).ready(function(){
jQuery('#finder').on('keypress',function(event) {
        alert(event.keyCode)
        if (event.keyCode == 13) {

           if (a.data.length > 0 ) {

               open_product(a.data[0]);

           }
        }
    });

})

WORKING DEMO 工作演示

NOTE: From w3school The order of events related to the keypress event: 注:从w3school起与按键事件相关的事件顺序:

    keydown - The key is on its way down
    keypress - The key is pressed down
    keyup - The key is released

However, the keypress event is not fired for all keys (eg ALT, CTRL, SHIFT, ESC). 但是,并非所有键(例如ALT,CTRL,SHIFT,ESC)都会触发keypress事件。 Use the keydown() method to also check these keys. 使用keydown()方法还可以检查这些键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM