jquery datepicker onselect 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1274347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:04:39  来源:igfitidea点击:

problem with jquery datepicker onselect

jquerydatepickeronselect

提问by hamzeh.hanandeh

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

按照 jquery 站点上的基本 onSelect 规范,我尝试了以下代码:

<!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??

并且无法让它工作!最初试图让更复杂的事情工作但 onSelect 只是不起作用所以我回到了基础但仍然不起作用,有人知道我做错了什么吗?

采纳答案by VoteyDisciple

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

您可以尝试删除该$("#datepicker").datepicker()行正上方的第二个,留下:

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

回答by Ken Browning

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

您没有正确使用 api。不要认为它不好,一开始它很混乱。

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:

如果要在初始化小部件后更改其中一个选项的值,则需要使用此 api:

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

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

或者,您可以使用 jQuery 的bind函数附加一个处理程序:

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

回答by hamzeh.hanandeh

Must this code work when select a date:

选择日期时此代码必须工作:

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

回答by Naveen Kumar

jQuery datepicker's onSelect doesn't work sometimes but you can just bypass it by calling a method on onchangeevent of your inputtype.

jQuery datepicker 的 onSelect 有时不起作用,但您可以通过在onchangeinput类型的事件上调用方法来绕过它。

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

Call this changeDate()method as-

将此changeDate()方法称为-

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