0

response_ajax.php我的页面使用以下代码得到响应:

<input class="btn" name="send_button" type="button" value="check"
  onClick=
    "xmlhttpPost('/response_ajax.php',
    'MyForm',
    'MyResult',
    '<img src=/busy.gif>')";
  return false;"
>

我得到回应;但是,jQuery 脚本不适用于到达的代码。我正在尝试添加scriptinside response_ajax.php,但没​​有任何反应:

<?php

// ... //

echo '

  <div id="whois-response">
    <pre>' .$str. '</pre>
  </div>

  <script>
    (function($){
      $(function(){
        alert("loaded");
      });
    })(jQuery);
  </script>

';  

?>

xmlhttpPost功能:

function xmlhttpPost(strURL,formname,responsediv,responsemsg) {

    var xmlHttpReq = false;

    var self = this;

    // Xhr per Mozilla/Safari/Ie7

    if (window.XMLHttpRequest) {

        self.xmlHttpReq = new XMLHttpRequest();

    }

    // per tutte le altre versioni di IE

    else if (window.ActiveXObject) {

        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");

    }

    self.xmlHttpReq.open('POST', strURL, true);

    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    self.xmlHttpReq.onreadystatechange = function() {

        if (self.xmlHttpReq.readyState == 4) {

            // Quando pronta, visualizzo la risposta del form

            updatepage(self.xmlHttpReq.responseText,responsediv);

        }

        else{

            // In attesa della risposta del form visualizzo il msg di attesa

            updatepage(responsemsg,responsediv);



        }

    }

    self.xmlHttpReq.send(getquerystring(formname));

}


function getquerystring(formname) {

    var form = document.forms[formname];

    var qstr = "";



    function GetElemValue(name, value) {

        qstr += (qstr.length > 0 ? "&" : "")

            + escape(name).replace(/\+/g, "%2B") + "="

            + escape(value ? value : "").replace(/\+/g, "%2B");

            //+ escape(value ? value : "").replace(/\n/g, "%0D");

    }



    var elemArray = form.elements;

    for (var i = 0; i < elemArray.length; i++) {

        var element = elemArray[i];

        var elemType = element.type.toUpperCase();

        var elemName = element.name;

        if (elemName) {

            if (elemType == "TEXT"

                    || elemType == "TEXTAREA"

                    || elemType == "PASSWORD"

                    || elemType == "BUTTON"

                    || elemType == "RESET"

                    || elemType == "SUBMIT"

                    || elemType == "FILE"

                    || elemType == "IMAGE"

                    || elemType == "HIDDEN")

                GetElemValue(elemName, element.value);

            else if (elemType == "CHECKBOX" && element.checked)

                GetElemValue(elemName, 

                    element.value ? element.value : "On");

            else if (elemType == "RADIO" && element.checked)

                GetElemValue(elemName, element.value);

            else if (elemType.indexOf("SELECT") != -1)

                for (var j = 0; j < element.options.length; j++) {

                    var option = element.options[j];

                    if (option.selected)

                        GetElemValue(elemName,

                            option.value ? option.value : option.text);

                }

        }

    }

    return qstr;

}


function updatepage(str,responsediv){

    document.getElementById(responsediv).innerHTML = str;

}
4

2 回答 2

0

我可能是错的,但我很确定你不能做多行字符串,除非它被配置为这样做(并运行较新版本的 PHP):

echo '

  <div id="whois-response">
    <pre>' .$str. '</pre>
  </div>

  <script>
    (function($){
      $(function(){
        alert("loaded");
      });
    })(jQuery);
  </script>

';  

尝试将其更改为:

echo <<<EOD

  <div id="whois-response">
    <pre> $str </pre>
  </div>

  <script>
    (function($){
      $(function(){
        alert("loaded");
      });
    })(jQuery);
  </script>

EOD;  

我认为您的 AJAX 响应是 PHP 错误,而不是您认为它正在返回的脚本。

于 2013-01-25T02:34:10.727 回答
0

通过将 jQuery 人员添加为函数来使其工作

function updatepage(str,responsediv){

    document.getElementById(responsediv).innerHTML = str;

    (function($){
      $(function(){
        $('html').my_jQuery_staff();
      });
    })(jQuery);

}

带有 jQ​​uery 的主要 JavaScript 文件:

// ~~ jQuery ~~
$(document).ready(function () {

    $.fn.my_jQuery_staff= function() {   
      return this.each(function() {   

        // Include jQuery staff here.

      });   
    };

    $('html').my_jQuery_staff();

});
于 2013-01-25T02:38:55.753 回答