jquery close datepicker when input lose focus

后端 未结 5 646
星月不相逢
星月不相逢 2020-12-06 16:41

I\'m using datepicker inside my input , my last field is the datepicker input , after validating it i want to set focus on another input inside my form , but the problem is

5条回答
  •  离开以前
    2020-12-06 17:21

    To be dynamic, and using the class assigned by jQueryui you can do:

    $(".hasDatepicker").on("blur", function(e) { $(this).off("focus").datepicker("hide"); });
    

    ;(function() {
    	function eventOnFocusDP(e, par) {
    		if (par.ver == $.fn.jquery) {
    			if (this.tmr) clearTimeout(this.tmr);
    			par.lbl1.text(par.msgs[1]);
    			this.tmr = setTimeout(function() { par.inpNP.focus(); }, par.secs*1e3);
    		}
    	}
    	
    	function eventOnFocusNP(e, par) {
    		if (par.ver == $.fn.jquery) {
    			par.lbl1.text(par.msgs[0]);
    			par.lbl2.text(par.msgs[2]);
    		}
    	}
    	
    	function eventOnBlurNP(e, par) {
    		if (par.ver == $.fn.jquery) par.lbl2.text("");
    	}
    	
    	function eventOnBlurHDP(e, par) {
    		if (par.ver == $.fn.jquery) {
    			$(this).off("focus").datepicker("hide");
    		}
    	}
    	
    	function test(secs) {
    		this.ver = $.fn.jquery;
    		
    		this.secs = (typeof secs)=='number'?secs:2;
    		this.msgs = [
    				'This will lose focus to box below '+this.secs+' seconds after it gains focus.',
    				'Losing focus in '+this.secs+' seconds!',
    				'Focus now on bottom box.'
    			];
    		
    		this.inpDP = $('[name=datePicker]');
    		this.inpNP = $('[name=nextPicker]');
    		this.lbl1 = $('#dPMsg').text(this.msgs[0]);
    		this.lbl2 = $('#dPMsg2');
    		var par = this;
    		
    		this.inpDP.datepicker({ dateFormat: "dd/mm/yy" })
    			.on('focus', function(e) { eventOnFocusDP.apply(this, [e, par]) });
    		this.inpNP.on('focus', function(e) { eventOnFocusNP.apply(this, [e, par]) });
    		this.inpNP.on('blur', function(e) { eventOnBlurNP.apply(this, [e, par]) });
    		$(document).on('blur', '.hasDatepicker', function(e) { eventOnBlurHDP.apply(this, [e, par]) });
    		return this;
    	}
    	
    	function init() {
    		window.Test = test;
    		setTimeout(function() {
              $(document).on('change', '.switcher, .switcher-ui', function(e) { if (window.Test) new Test(); });
              $(jQueryUISwitcher).trigger('change');
            }, 1e3);
    	}
    	
    	if (document.readyState == "complete") init();
    	else jQuery(window).on('load', init);
    })();
    
    
    




提交回复
热议问题