简体   繁体   English

Jquery Datepicker onselect调用函数

[英]Jquery Datepicker onselect call function

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. 如果我取出onSelect: showUser()工作正常,但不发布数据,但如果我包括onselect,则datepicker不起作用。 no drop down. 没有下降。

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? 我该怎么办才能让它发挥作用?

jsFiddle Demo

The main issue is that the function showUser is not defined in a scope available to the scope where the datepicker is assigned. 主要问题是函数showUser未在分配了datepicker的作用域可用的作用域中定义。 The result is more than likely an error that you can see in your console: "Uncaught ReferenceError: showUser is not defined ". 结果很可能是您在控制台中看到的错误:“未捕获的ReferenceError:未定义showUser”。 When the error is caused all of the script execution is halted and the datepicker is not constructed. 导致错误时,将暂停所有脚本执行并且不构造datepicker。 You should make sure that the datepicker object has access to this function. 您应该确保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的返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM