javascript Jquery Datepicker onselect 调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21126073/
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
Jquery Datepicker onselect call function
提问by Mike
This is a function I need to call when my JQuery date picker selects both to and from dates:
当我的 JQuery 日期选择器同时选择起始日期和起始日期时,我需要调用这个函数:
function showUser() {
// Retrieve values from the selects
var DateFrom = document.getElementById('DateFrom').value;
var DateTo = document.getElementById('DateTo').value;
if (dtd=="" || dtm == "" || dfd == "" || dfm == "") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/StreetHailDrivers.php?DateFrom="+DateFrom+"&DateTo="+DateTo,true);
xmlhttp.send();
}
and this is my datepicker:
这是我的日期选择器:
$(function() {
$('.datepicker').datepicker({ onSelect: showUser(), minDate: -90, maxDate: "+1D" });
});
If i take out the onSelect: showUser()
The datepicker works fine, but doesnt post the data, but if i include the onselect the datepicker doesnt work. no drop down.
如果我取出onSelect: showUser()
日期选择器工作正常,但不发布数据,但如果我包含 onselect 日期选择器不起作用。没有下降。
I also tried the onchange event in the html:
我还尝试了 html 中的 onchange 事件:
<input type="text" class="datepicker" name="DateTo" id="DateTo" onchange="showUser()" />
what should I do to get it working?
我该怎么做才能让它工作?
回答by Travis J
The main issue is that the function showUser is not defined in a scope available to the scope where the datepicker is assigned. The result is more than likely an error that you can see in your console: "Uncaught ReferenceError: showUser is not defined ". When the error is caused all of the script execution is halted and the datepicker is not constructed. You should make sure that the datepicker object has access to this function.
主要问题是函数 showUser 未在分配日期选择器的范围可用的范围内定义。结果很可能是您在控制台中看到的错误:“Uncaught ReferenceError: showUser is not defined”。当导致错误时,所有脚本执行都将停止并且不构造日期选择器。您应该确保 datepicker 对象可以访问此函数。
I believe it is expecting a pointer to a function for that parameter. As such it should probably be
我相信它需要一个指向该参数的函数的指针。因此它应该是
onSelect: showUser
Using onSelect: showUser()
will set the value of onSelect
to the return value of showUser
which is undefined.
使用onSelect: showUser()
会将 的值设置为未定义onSelect
的返回值showUser
。