【问题标题】:clickable JS functions after PHP+Ajax innerHTML not workingPHP + Ajax innerHTML 后可点击的 JS 函数不起作用
【发布时间】:2011-12-22 15:31:36
【问题描述】:

首先:我不想使用 JQuery,我知道它会更容易,但它会破坏这件事的乐趣。 注意:ajaxFunction 是 GET/POST 的默认 ajax 示例

现在,在第一次单击该功能后,它确实起作用了,并且everyrhing 被 php-echo 替换了。问题是在之后。

将 initia-div ("login-place") 更改为下一个 div ("regit") 后,sregit.onClick = function() 的 javascript 函数不起作用。

我该如何解决这个问题?我应该在 PHP 中创建 DOMElement 并使用 xmlSave("index.php") 吗?

初始-div:

<div id="login-place">
    <table id="login-init" name="login-init">
    <tr>
        <td>username</td>
        <td><input type="text" name="user" id="user" /></td>
        <td>password</td>
        <td><input type="password" name="pass" id="pass" /></td>
    </tr>
    <tr>
        <td colspan="2"><input name="login" id="login" type="button" value="entrar" /></td>
    </tr>
    </table>
</div>

JS代码:

            var slogin = document.getElementById("login");
            slogin.onclick = function() {
                //alert("inside."+user.value);
                s = "goforth=1&user="+user.value+"&pass="+pass.value;
                ajaxFunction('4',""+s); 
            }

            var sregit = document.getElementById("regit");
            sregit.onclick = function () {
                alert("inside."+user.value);
                s = "regit=1&user="+user.value+"&pass="+pass.value;
                ajaxfunction('4',""+s); 
            }
    function ajaxFunction(arg,string) {
      var getdate = new Date();  //Used to prevent caching during ajax call
      if(xmlhttp) {

          if (arg==4) {
            nextland = "login-place";
            document.getElementById("all-burps").innerHTML=string;
            xmlhttp.open("POST","session.php",true);
          }
        xmlhttp.onreadystatechange  = handleServerResponse;
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        if (arg==4) {
            xmlhttp.setRequestHeader('Content-length', string.length);
            xmlhttp.send(""+string); 
        }
        }
    }
function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       if (nextland != "login-place") { document.getElementById(nextland).innerHTML=xmlhttp.responseText; } //Update the HTML Form element
       else {
           // alert(xmlhttp.responseText); -> this is true no error up here.
           var thediv = document.getElementById("login-init"); 
           thediv.parentNode.removeChild(thediv); //this happens fine.
           var theplace = document.getElementById("login-place");
           var thelement = document.createElement("div"); //this too.
           thelement.innerHTML=xmlhttp.responseText;  //this too'
           theplace.appendChild(thelement); //this too.

       }
     }

而且,innerhtml.responseText 是 =

        echo '
            <table id="login-2" name="login-2">
            <h3>o username ' . $username . ' não existe. <a href="#" id="register-link" name="register-link">Queres registar?</a></h3>
            <tr>
                <td>username</td>
                <td><input type="text" name="user" id="user" value="' . $username . '"/></td>
            <td>password</td>
                <td><input type="password" name="pass" id="pass" value="' . $password . '" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="button" value="entrar" id="regit" name="regit"/></td>
            </tr>
        </table>
        ';

我希望这足以让任何人理解我的疑问。否则我会改写它。

【问题讨论】:

    标签: php javascript ajax forms innerhtml


    【解决方案1】:

    函数中的调用无效ajaxfunction,而函数调用ajaxFunction - Javascript 区分大小写。

    另外,你需要将regitonclick函数声明移动到handleServerResponse()函数内部,在thelement.innerHTML=xmlhttp.responseText;行之后,因为在调用此行之前,没有带有@987654327的元素@,所以函数的绑定不起作用。

    此外,您应该使用; 终止声明为element.onclick = function () {} 的函数。以这种方式声明函数是一个赋值语句,因此它需要一个分号来终止它。

    编辑这里是您的代码重新编写,因此希望它可以满足您的要求,并消除了一些额外的臃肿。我假设 xmlhttpnextlanduserpass 已在您尚未发布的某些代码中声明 - 是吗?

    document.getElementById("login").onclick = function() {
      //alert("inside."+user.value);
      var s = "goforth=1&user="+encodeURIComponent(user.value)+"&pass="+encodeURIComponent(pass.value);
      ajaxFunction(4, s); 
    };
    
    function ajaxFunction(arg, string) {
      //var getdate = new Date();  //Used to prevent caching during ajax call
      // ...but never actually *used* anywhere...
      if (xmlhttp) {
        if (arg == 4) {
          nextland = "login-place";
          document.getElementById("all-burps").innerHTML = string;
          xmlhttp.open("POST", "session.php", true);
        }
        xmlhttp.onreadystatechange = handleServerResponse;
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        if (arg == 4) {
          xmlhttp.setRequestHeader('Content-length', string.length);
          xmlhttp.send(string); 
        }
      }
    }
    
    function handleServerResponse() {
      var theplace, thelement;
      if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
          if (nextland != "login-place") {
            // Update the HTML Form element
            document.getElementById(nextland).innerHTML = xmlhttp.responseText;
          } else {
            // Remove the old div and add the new content
            theplace = document.getElementById("login-place");
            theplace.removeChild(document.getElementById("login-init"));
            thelement = document.createElement("div");
            thelement.innerHTML = xmlhttp.responseText;
            theplace.appendChild(thelement); //this too.
            // Now add the click handler
            document.getElementById("regit").onclick = function() {
              // alert("inside."+user.value);
              var s = "regit=1&user="+encodeURIComponent(user.value)+"&pass="+encodeURIComponent(pass.value);
              ajaxfunction(4, s); 
            };
          }
        } else {
          // Handle HTTP errors
          alert('HTTP ' + xmlhttp.status);
        }
      }
    }
    

    【讨论】:

    • 你无法想象我现在对你有多少爱和温柔。感谢“Javascript 区分大小写”,我打赌这需要一个小时才能找到。此外,我应该在某事物存在之后为其分配一个声明,这完全有道理。谢谢你,你把我的头拿掉的时间已经不够用了。
    • 你不应该 :) 是的,它们都是以前设置的。另外,我今天学习了一个新功能 encodeURIComponent :P 你对我的代码做了什么?那个整洁的文本块不可能和我的代码一样 xD [膨胀不是膨胀,还有更多的参数:P]
    【解决方案2】:

    尝试调用

    "sregit.onclick = function () { ...."

    在handleServerResponse() 函数中附加内容之后。之前调用它时,不会绑定任何操作,因为 DOM 中没有 #regit 元素。

    【讨论】:

    • 我给了你你的第一点!胡扎:D
    【解决方案3】:

    当您像这样修改 HTML 时,onClick 事件会丢失。每次替换 HTML 或使用 onClick 内联(如 onclick="something()")时,您都必须重新添加 onClick 事件。

    您还有一个语法错误,您在 ajaxfunction('4',""+s); 中使用了小写 F,而函数名为 ajaxFunction

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多