简体   繁体   English

如何将文档准备功能放置在ajax函数中

[英]How to place a document ready function inside an ajax function

I have used the datepicker function on 3 other pages and all works great, but now I am trying to add it to a page that is produced based on an ajax function call showbookings. 我在其他3个页面上使用了datepicker函数,并且一切正常,但是现在我试图将其添加到基于ajax函数调用showbookings生成的页面中。 There are currently 8 on click functions that are being reattached after it loads the dynamic content, but I can not get the datepicker to work, show, nothing. 当前有8个单击功能在加载动态内容后将重新连接,但是我无法使datepicker正常工作,显示,什么也没有。 So how do you take a $(document).ready(function and place it into the ajax call? I have my input class = "datepicker1" 那么,如何使用$(document).ready(function并将其放入ajax调用中呢?我有输入类=“ datepicker1”

function showbookings(str, pass) {
    if (str === "") {
        document.getElementById("txtBookings").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("txtBookings").innerHTML = xmlhttp.responseText;

        *********

        }
    };

    xmlhttp.open("GET", "page1.php?q=" + str, true);
    xmlhttp.send();
}





$(document).ready(function () {
    $(".datepicker1").datepicker({
        showOn: 'both',
        buttonImage: 'images/calendar.gif',
        buttonText: 'Pick Date',
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true,
        showAnim: 'slideDown',
        duration: 'fast',
        onClose: function (text, inst) {
            $(this).focus();
        }
    });
});

So how do you take a $(document).ready(function and place it into the ajax call? 那么,如何使用$(document).ready(函数并将其放入ajax调用中?

You don't. 你不知道 It would be meaningless in that context. 在这种情况下,这将毫无意义。 It probably wouldn't even do anything, if the ready event doesn't fire again for the document after the AJAX call. 如果在AJAX调用之后ready事件不再为document触发,它甚至可能什么也不做。 Since all that code does is attach an event handler to that event, it doesn't actually execute the code within the handler until the event fires. 由于所有代码都将事件处理程序附加到该事件,因此在事件触发之前,它实际上不会在处理程序内执行代码。

It sounds like what you actually want to do is initialize the date picker plugin on elements added to the DOM after an AJAX call. 听起来您实际上想要做的是在AJAX调用之后初始化添加到DOM的元素上的日期选择器插件。 You can do this by just calling the plugin initializer again: 您可以通过再次调用插件初始化程序来做到这一点:

$(".datepicker1").datepicker({
    showOn: 'both',
    buttonImage: 'images/calendar.gif',
    buttonText: 'Pick Date',
    buttonImageOnly: true,
    changeMonth: true,
    changeYear: true,
    showAnim: 'slideDown',
    duration: 'fast',
    onClose: function (text, inst) {
        $(this).focus();
    }
});

Note that this will match all '.datepicker' elements in the DOM. 请注意,这将匹配DOM中的所有 '.datepicker'元素。 Including the ones that were previously initialized. 包括先前初始化的那些。 If this causes undesirable behavior on previously initialized elements, then just restrict the selector for the plugin to include only the new elements. 如果这导致了先前初始化的元素上的不良行为,则只需将插件的选择器限制为仅包含新元素。 From the looks of your code, that might be something like: 从代码的外观来看,可能类似于:

$("#txtBookings .datepicker1").datepicker({
    showOn: 'both',
    buttonImage: 'images/calendar.gif',
    buttonText: 'Pick Date',
    buttonImageOnly: true,
    changeMonth: true,
    changeYear: true,
    showAnim: 'slideDown',
    duration: 'fast',
    onClose: function (text, inst) {
        $(this).focus();
    }
});

Basically you'd just need to adjust the selector to specify only the elements which were added, which presumably have some common parent element. 基本上,您只需要调整选择器以仅指定添加的元素,这些元素大概具有一些公共的父元素。

Since you are using jQuery, it is better to use it for handling the AJAX too: 由于您使用的是jQuery,因此最好也使用它来处理AJAX:

function showbookings(str, pass) {
    if (str === "") {
        $("#txtBookings").html("");
        return;
    }
    $.get(page1.php?q=" + str);
}

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

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