首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP+Ajax innerHTML不工作后可单击的JS函数

PHP+Ajax innerHTML不工作后可单击的JS函数
EN

Stack Overflow用户
提问于 2011-12-22 23:31:36
回答 3查看 1.1K关注 0票数 1

首先:我不想使用JQuery,我知道它会让它变得更容易,但它会让人着迷。注意: ajaxFunction是GET/POST的默认ajax示例

现在,在第一次单击该函数后,它可以正常工作,并且所有内容都会被php-echo替换。问题出在后头。

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

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

initial-div:

代码语言:javascript
复制
<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代码:

代码语言:javascript
复制
            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为=

代码语言:javascript
复制
        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>
        ';

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-12-22 23:41:42

不起作用的函数中的调用是ajaxfunction,而函数被称为ajaxFunction - Javascript是区分大小写的。

此外,还需要将regitonclick函数的声明移到handleServerResponse()函数中的thelement.innerHTML=xmlhttp.responseText;行之后,因为在调用此行之前,没有id="regit"元素,因此函数的绑定将不起作用。

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

编辑这里是你重新编写的代码,所以希望它能做你想要的,去掉一些多余的臃肿。我假设xmlhttpnextlanduserpass已经在一些你还没有发布的代码中声明了--是吗?

代码语言:javascript
复制
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);
    }
  }
}
票数 2
EN

Stack Overflow用户

发布于 2011-12-22 23:41:40

试着拨打

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

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

票数 1
EN

Stack Overflow用户

发布于 2011-12-22 23:41:17

当你这样修改超文本标记语言时,onClick事件就会丢失。您必须在每次替换HTML时重新添加onClick事件,或者使用像onclick="something()"这样的onClick内联。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8606052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档