Home > Article > Web Front-end > jquery implements methods to support enter or focus selection by extending the select control_jquery
The example in this article describes how jquery supports enter or focus selection by extending the select control. Share it with everyone for your reference, the details are as follows:
/*************************************** * @ author jdkleo * @ date 2013/2/27 JQuery SelKeys USAGE: jQuery.selkeys.enter(jQuery("#selcon")); jQuery.selkeys.focus(jQuery("#selcon2")); *****************************************/ (function (jQuery){ this.version = '(beta)(0.0.1)'; this.all = {}; /**---------enter---------**/ this.enter = function(sel){ var flag = 1; var open = function(){ if(flag==1){ sel.get(0).size = sel.get(0).options.length; flag=0; }else{ sel.get(0).size = 1; flag=1; } }; sel.keydown(function(e){ e = e ? e :(window.event ? window.event : null); var code = e.keyCode || e.which || e.charCode; if(code == 13) { open(); return false; } }); sel.blur(function(){ sel.get(0).size=1; flag=1; }); } /**---------focus---------**/ this.focus = function(sel){ var flag = 1; var open = function(){ if(flag==1){ sel.get(0).size = sel.get(0).options.length; flag=0; }else{ sel.get(0).size = 1; flag=1; } }; sel.focus(function(){ open(); return false; }); sel.blur(function(){ sel.get(0).size=1; flag=1; }); sel.keydown(function(e){ e = e ? e :(window.event ? window.event : null); var code = e.keyCode || e.which || e.charCode; if(code == 13) { sel.get(0).size=1; flag=1; return false; } }); } /**---------all---------**/ jQuery.selkeys = this; return jQuery; })(jQuery);
I hope this article will be helpful to everyone in jQuery programming.