简体   繁体   中英

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:

      <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:

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

  $.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:

    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). Use the keydown() method to also check these keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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