首先:我不想使用JQuery,我知道它会让它变得更容易,但它会让人着迷。注意: ajaxFunction是GET/POST的默认ajax示例
现在,在第一次单击该函数后,它可以正常工作,并且所有内容都会被php-echo替换。问题出在后头。
将initia ("login-place")更改为下一个div ("regit")后,sregit.onClick = function()的javascript函数不起作用。
我该如何解决这个问题呢?我应该在PHP中创建DOMElement并使用xmlSave("index.php")吗?
initial-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>
';我希望这足以让任何人理解我的怀疑。否则我会改写的。
发布于 2011-12-22 23:41:42
不起作用的函数中的调用是ajaxfunction,而函数被称为ajaxFunction - Javascript是区分大小写的。
此外,还需要将regit的onclick函数的声明移到handleServerResponse()函数中的thelement.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);
}
}
}发布于 2011-12-22 23:41:40
试着拨打
"sregit.onclick = function () { ....“
在handleServerResponse()函数中追加内容之后。当您之前调用它时,没有绑定任何操作,因为在DOM中没有#regit元素。
发布于 2011-12-22 23:41:17
当你这样修改超文本标记语言时,onClick事件就会丢失。您必须在每次替换HTML时重新添加onClick事件,或者使用像onclick="something()"这样的onClick内联。
你还有一个语法错误,你在ajaxfunction('4',""+s);中使用了小写的F,而这个函数被命名为ajaxFunction。
https://stackoverflow.com/questions/8606052
复制相似问题