2014-11-14 1 views
6

Sto usando i tasti freccia SU e GIÙ per spostarti tra i valori di input.jQuery - Nascondi jQdialog su keypress a partire da .datepicker

sto cercando di nascondere il .datepicker quando si preme il tasto freccia su o giù come segue: -

$('#booking-docket').keyup(function (e) { 

    if(e.keyCode === 38 && $("#txt-date").is(":focus") || e.keyCode === 40 && $("#txt-date").is(":focus")) { 

     $('#txt-date').datepicker("hide"); 

    } 

}); 

Quello che sta accadendo è che, non appena l'elemento #txt-date è attiva, si apre velocemente e chiude il datepicker, ho pensato aggiungendo .is(:focus) risolverebbe questo, ma non è questo il caso.

Cosa mi sembra che manchi? (Non so se riesco a nascondere il DatePicker SOLO quando la pressione del tasto sta andando via da un campo di testo?)

CODICE HTML

 <div class="booking-right left"> 

      <div class="col-1 left"> 

       <p class="p-lbl-txt left">TELEPHONE:</p> 

       <input type="text" id="txt-telephone" class="input-txt-sml move right" tabindex="5" /> 

      </div> 

      <div class="col-2 left"> 

       <p class="p-lbl-txt left">DATE:</p> 

       <input type="text" id="txt-date" class="input-txt-sml move right" tabindex="7" /> 

      </div> 

      <div class="col-1 left"> 

       <p class="p-lbl-txt left">LEAD TIME:</p> 

       <input type="text" id="txt-lead" class="input-txt-sml move right" tabindex="6" /> 

      </div> 

      <div class="col-2 left"> 

       <p class="p-lbl-txt left">TIME:</p> 

       <input type="text" id="txt-min" class="input-txt-xxsml move right" tabindex="9" /> 

       <input type="text" id="txt-hour" class="input-txt-xxsml move right" tabindex="8" /> 

      </div>    

     </div> 

Ho provato quanto segue ma non poteva farlo lavoro: -

$(function() { 
    $("#txt-date").datepicker({ dateFormat: "dd/mm/yy" }); 
}); 

$('#booking-docket').keyup(function (e) { 
    /*Add parentheses in your `if` statement to separate the `AND` and `OR`:*/ 
    if ((e.keyCode === 38 && $("#txt-date").is(":focus"))) { 
     $("#txt-lead").focus();//change focus to an other input 
     $('#txt-date').datepicker("hide");//hide the datepicker 
    } 
}); 
+0

Puoi anche inserire il codice HTML? – Scimonster

+0

@Scimonster - aggiunto questo per voi – nsilva

risposta

4

si può usare qualcosa di simile:

JS:

$('#txt-date').keyup(function (e) { 
    if ((e.keyCode === 38 && $("#txt-date").is(":focus")) || (e.keyCode === 40 && $("#txt-date").is(":focus"))) { 

     if (e.keyCode === 38) $("#txt-telephone").focus(); 
     else if (e.keyCode === 40) $("#txt-lead").focus(); 

     $('#txt-date').datepicker("hide"); 

    } 
}); 

/*Add the datepicker*/ 
$(function() { 
    $("#txt-date").datepicker(); 
}); 

JSFIDDLE: http://jsfiddle.net/ghorg12110/ghwvvkve/2/

+0

Grazie @ Magicprog.fr - Appena provato ma non sembra funzionare con l'HTML che sto usando, ho aggiornato la mia domanda – nsilva

+0

@nsilva answer e jsfiddle aggiornato –

+0

Perfetto! Thankyou @ Magicprog.fr - Non posso assegnare i punti per 20 ore però :-) – nsilva