I am using jQueryUI date picker, and when the user clicks on the text field and presses the enter key, the current date is populated. I want to avoid this. I tried this:
$('#datepicker').on('keypress', function(e){ if (e.which == 13) { e.preventDefault(); e.stopPropagation(); return false; } });
Bad luck
here is the demo link https://jsfiddle.net/shalini456/zwjzo175/
Try the following:
$("#datepicker").keydown(myfunction); // use keydown function myfunction(e) { if(e.keyCode === 13) { e.stopPropagation(); e.preventDefault(); return false; } }
$('#datepicker').keypress(function(e){ if (e.keyCode == 10 || e.keyCode == 13) //10 is Line Feed and 13 is Carriage Return e.preventDefault(); });
, ... "#datepicker" https://jsfiddle.net/shalini456/zwjzo175/4/
"#datepicker"
JQuery
$(document).ready(function(){ $( "#datepicker" ).datepicker(); $("#datepicker").on('click', function(){ $("#datepicker").blur(); }); });
Bhupesh Kushwaha,
$(function() { $("input").bind('keydown', function (e) { if (event.which == 13) { $(this).trigger(e); return false; } }).datepicker(); });
: https://jsfiddle.net/zwjzo175/27/
,
, , , , , "Charly H", datepicker.
, , .
:
$(function() { $("input").bind('keydown', function (e) { if (event.which == 13) { e.preventDefault(); e.stopPropagation(); $(this).trigger(e); return false; } }).datepicker(); });
, .
, , , , .
As mentioned in "Charly H", keydown binding must occur before calling datepicker.
Then in the call to the keydown handler e.stopImmediatePropagation(). For instance.
e.stopImmediatePropagation()
$('#datepicker').bind('keydown',function(e){ if (e.keyCode == 10 || e.keyCode == 13) //10 is Line Feed and 13 is Carriage Return e.stopImmediatePropagation(); }) .datepicker();
Fiddle: https://jsfiddle.net/8fyvh8wd/2/