problem with jquery datepicker onselect

南楼画角 提交于 2019-11-28 07:07:40

问题


Following the basic onSelect spec on the jquery site I tried the following code:

<!DOCTYPE html>
<html>
<head>
  <link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" />
  <script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
  <script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.core.js"></script>
  <script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.datepicker.js"></script>
  <script type="text/javascript">
  $(document).ready(function(){
    $("#datepicker").datepicker();


$('#datepicker').datepicker({
   onSelect: function(dateText, inst) { alert("Working"); }
});



  });

  </script>
</head>
<body style="font-size:62.5%;">

<div type="text" id="datepicker"></div>

</body>
</html>

And can't get it to work! Originally been trying to get a more complicated thing to work but the onSelect was just not working so I went back to basics and still don't work, anyone any idea what I'm doing wrong??


回答1:


You might try removing the second $("#datepicker").datepicker() immediately above that line, leaving:

$(document).ready(function(){
    $('#datepicker').datepicker({ onSelect: function(dateText, inst) { alert("Working"); } });
});



回答2:


You're not using the api correctly. Don't take it bad, it's confusing at first.

Both of these lines are telling the datepicker widget to initialize a datepicker on an element:

$("#datepicker").datepicker();
$('#datepicker').datepicker({
   onSelect: function(dateText, inst) { alert("Working"); }
});

You could remove the first one since it's not doing anything except preventing the second one from having any effect.

If you want to change the value of one of the options after you've already initialized the widget then you would need to use this api:

$('#datepicker').datepicker('option', 'onSelect', function() { /* do stuff */ });

Or alternatively, you could attach a handler using jQuery's bind function:

$('#datepicker').bind('onSelect', function() { /* do stuff */ });



回答3:


Must this code work when select a date:

$("#datepicker").datepicker({
    dateFormat: 'dd/mm/yy'
}).on("changeDate", function (e) {
    alert("Working");
});



回答4:


jQuery datepicker's onSelect doesn't work sometimes but you can just bypass it by calling a method on onchange event of your input type.

function changeDate() {
   $("#datepicker").datepicker(
        "change",
        { 
            alert("I am working!!");
        }
    );
}

Call this changeDate() method as-

<div type="text" id="datepicker" onchange ="javascript:changeDate();" />


来源:https://stackoverflow.com/questions/1274347/problem-with-jquery-datepicker-onselect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!