PHP+;之后可单击的JS函数;Ajax innerHTML不工作
首先:我不想使用JQuery,我知道这会让它变得更简单,但它会让事情变得一团糟。 注意:ajaxFunction是GET/POST的默认ajax示例 现在,在第一次单击该函数之后,它就可以工作了,并且所有的内容都被php echo替换。问题在后面 将initia div(“登录位置”)更改为下一个div(“regit”)后,sregit.onClick=function()的javascript函数不起作用 我该怎么做?我应该用PHP创建DomeElement并使用xmlSave(“index.PHP”)吗 初始分区:PHP+;之后可单击的JS函数;Ajax innerHTML不工作,php,javascript,ajax,forms,innerhtml,Php,Javascript,Ajax,Forms,Innerhtml,首先:我不想使用JQuery,我知道这会让它变得更简单,但它会让事情变得一团糟。 注意:ajaxFunction是GET/POST的默认ajax示例 现在,在第一次单击该函数之后,它就可以工作了,并且所有的内容都被php echo替换。问题在后面 将initia div(“登录位置”)更改为下一个div(“regit”)后,sregit.onClick=function()的javascript函数不起作用 我该怎么做?我应该用PHP创建DomeElement并使用xmlSave(“index.
<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>
并且,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>
';
echo'
o用户名'$用户名。”不存在。
用户名
密码
';
我希望这足以让任何人理解我的怀疑。否则我将重写它。当您这样修改HTML时,onClick事件将丢失。每次替换HTML或使用类似
onClick=“something()”
的onClick内联时,都必须重新添加onClick事件
您还有一个语法错误,在ajaxfunction('4',“”+s)中使用了小写的F代码>,而函数名为ajaxFunction
请尝试调用
“sregit.onclick=函数(){….”
在handleServerResponse()函数中添加内容后。当您之前调用它时,不会绑定任何操作,因为DOM中没有#regit元素。在不起作用的函数中调用的是ajaxfunction
,而调用的是ajaxfunction
-Javascript区分大小写
另外,您需要将regit
的onclick
函数的声明移动到handleServerResponse()
函数中thement.innerHTML=xmlhttp.responseText;
行之后,因为在调用此行之前,没有带有id=“regit”的元素
,因此函数的绑定将不起作用
此外,还应使用;
终止声明为element.onclick=function(){}
的函数。以这种方式声明函数是赋值语句,因此需要分号来终止它
编辑这是你的代码,希望它能做你想做的事情,去掉一些多余的赘肉。我假设xmlhttp
、nextland
、user
和pass
已经在一些你没有发布的代码中声明了,是吗
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区分大小写”我打赌要花上一个小时才能找到答案。另外,在某个东西存在之后,我应该给它分配一个语句是完全有道理的。谢谢你,在你把我的头摘下来的时候,没有足够的单词了。你不应该:)是的,它们都是以前设置的。另外,我今天学习了一个新函数:P,你对我的组件做了什么代码?那个整洁的文本块不可能与我的代码相同xD[膨胀不是膨胀,还有更多的args:P]我给了你你的第一点!HUZZAH:D
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);
}
}
}