gpt4 book ai didi

php - PHP + Ajax innerHTML 后可点击的 JS 函数不起作用

转载 作者:行者123 更新时间:2023-11-30 13:25:30 24 4
gpt4 key购买 nike

首先:我不想使用 JQuery,我知道它会使它变得更容易,但它会破坏事物的海豚。注意:ajaxFunction 是 GET/POST 的默认 ajax 示例

现在,在单击该函数后,第一次它确实起作用并且 everyrhing 被 php-echo 取代。问题在之后。

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

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

初始分区:

<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>
';

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

最佳答案

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

此外,您需要将regitonclick 函数的声明移动到handleServerResponse() 函数内部,在thelement.innerHTML=xmlhttp.responseText;,因为在调用这行之前,没有id="regit"的元素,所以函数的绑定(bind)不起作用.

此外,您应该使用 ; 终止声明为 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);
}
}
}

关于php - PHP + Ajax innerHTML 后可点击的 JS 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8606052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com