te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to disable enter key inside jqueryui datepicker - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disable enter key inside jqueryui datepicker - Stack Overflow

programmeradmin3浏览0评论

I am using jQueryUI date picker and when user clicks on a textbox and hits the enter key the current date gets populated. I want to avoid that. I have tried this:

$('#datepicker').on('keypress',  function(e){
    if (e.which == 13) {
        e.preventDefault();
        e.stopPropagation();
        return false; 
    }
});

With no luck

here is the link of demo /

I am using jQueryUI date picker and when user clicks on a textbox and hits the enter key the current date gets populated. I want to avoid that. I have tried this:

$('#datepicker').on('keypress',  function(e){
    if (e.which == 13) {
        e.preventDefault();
        e.stopPropagation();
        return false; 
    }
});

With no luck

here is the link of demo https://jsfiddle/shalini456/zwjzo175/

Share Improve this question edited Mar 5, 2016 at 6:10 Shalini asked Mar 4, 2016 at 19:36 ShaliniShalini 1631 gold badge1 silver badge10 bronze badges 6
  • Possible duplicate of Disable enter key in JQuery ui datepicker – Charly H Commented Mar 4, 2016 at 19:52
  • Possible duplicate of jQuery: Prevent enter key – Maxime Savard Commented Mar 4, 2016 at 20:04
  • @CharlyH before posting my question i did try the solutions mentioned in SO but it doesnt work see jsfiddle/shalini456/zwjzo175/2 – Shalini Commented Mar 5, 2016 at 6:16
  • @MaximeSavard the solution mentioned in that thread doesnt work for me :( – Shalini Commented Mar 5, 2016 at 6:16
  • @Shalini look at my answer or at the solution from Bhupesh Kushwaha in the ments – Charly H Commented Mar 7, 2016 at 8:35
 |  Show 1 more ment

6 Answers 6

Reset to default 6

Try this:

$("#datepicker").keydown(myfunction); // use keydown

function myfunction(e) {
    if(e.keyCode === 13) {
        e.stopPropagation();
        e.preventDefault();

        return false;
    }
}

Some of the solutions here appear to work because they throw an exception of some kind.

There are two things which seem to make it work without throwing an exception.

As mentioned by "Charly H" the keydown binding must happen before calling datepicker.

Then in the keydown handler call e.stopImmediatePropagation(). E.g.

  $('#datepicker').bind('keydown',function(e){

    if (e.which == 13)
        e.stopImmediatePropagation();
  })
  .datepicker();

Fiddle: https://jsfiddle/8fyvh8wd/18/

Try this :

$('#datepicker').keypress(function(e){

    if (e.keyCode == 10 || e.keyCode == 13) //10 is Line Feed and 13 is Carriage Return
        e.preventDefault();

  });

If you cant disable the enter keypress there is a work around for this... you can just remove the focus from "#datepicker" check the following updated fiddle https://jsfiddle/shalini456/zwjzo175/4/

jQuery

$(document).ready(function(){
    $( "#datepicker" ).datepicker();

  $("#datepicker").on('click', function(){
   $("#datepicker").blur();
  });


});

I've shortened down the solution of Bhupesh Kushwaha, which he provided in the ments

$(function() {
    $("input").bind('keydown', function (e) {
    if (event.which == 13) {
    $(this).trigger(e); 
    return false; 
  } 
    }).datepicker(); 
  });

This worked for me: https://jsfiddle/zwjzo175/27/

guys,

I know that this might be too late, but the only solution that worked for me was the one from "Charly H", where he put the code binding the input text before the declaration of the datepicker.

I guess the reason is that the bubble propagation and the priority of it.

The code would look like this:

$(function() {
    $("input").bind('keydown', function (e) {
        if (event.which == 13) {
            e.preventDefault();
            e.stopPropagation();
            $(this).trigger(e); 
            return false;
        } 
    }).datepicker(); 
});

发布评论

评论列表(0)

  1. 暂无评论