PHP+Ajax innerHTML 不起作用后可点击的 JS 函数
首先:我不想使用 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>
';
我希望这足以让任何人理解我的疑问。否则我会重写它。
First off: I do not want to use JQuery, i know it would make it easier but it would ravish the porpuse of the thing.
Note: ajaxFunction is the default ajax exemple for a GET/POST
Now, after clicking the function, the first time, it does work and everyrhing gets replaced by the php-echo. The problem is AFTER.
After changing the initia-div ("login-place") to the next div ("regit") the javascript function of sregit.onClick = function() does not work.
How do i go around this? Should i create the DOMElement in PHP and use 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>
the JS code:
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.
}
}
And, the innerhtml.responseText is =
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>
';
I hope this is enough for anyone to understand my doubt. else i'll reword it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不起作用的函数中的调用为
ajaxfunction
,而该函数名为ajaxFunction
- JavaScript 区分大小写。此外,您需要将
regit
的onclick
函数声明移动到handleServerResponse()
函数内部,位于thelement 行之后.innerHTML=xmlhttp.responseText;
,因为在调用此行之前,没有id="regit"
的元素,所以函数的绑定不起作用。另外,您应该使用
;
终止声明为element.onclick = function () {}
的函数。以这种方式声明函数是一个赋值语句,因此需要一个分号来终止它。编辑这是您重新编写的代码,希望它能达到您想要的效果,并删除一些额外的臃肿。我假设
xmlhttp
、nextland
、user
和pass
已在您尚未发布的某些代码中声明- 有吗?The call in the function that does not work says
ajaxfunction
, whereas the function is calledajaxFunction
- Javascript is case sensitive.Also, you need to move the declaration of the
onclick
function forregit
to inside thehandleServerResponse()
function, after the linethelement.innerHTML=xmlhttp.responseText;
, because before this line is called, there is no element withid="regit"
, so the binding of the function won't work.Also, you should terminate the functions declared as
element.onclick = function () {}
with a;
. Declaring functions in this manner is an assignment statement and as such it requires a semi-colon to terminate it.EDIT here is your code reworked so hopefully it does what you want, with some extra bloat removed. I am assuming that
xmlhttp
,nextland
,user
andpass
have been declared in some code you haven't posted - have they?尝试调用
“sregit.onclick = function () { ....”
在handleServerResponse()函数中添加内容后 。当您之前调用它时,不会绑定任何操作,因为 DOM 中没有 #regit 元素。
Try to call
"sregit.onclick = function () { .... "
after appending content in handleServerResponse() function. When you call it earlier, no action is binded, because there is no #regit element in DOM.
当您像这样修改 HTML 时,onClick 事件就会丢失。每次替换 HTML 或使用 onClick 内联(如
onclick="something()"
)时,您都必须重新添加 onClick 事件。您还存在语法错误,您在
ajaxfunction('4',""+s);
中使用了小写 F,而该函数名为ajaxFunction
。The onClick events are lost when you modify the HTML like that. You'll have to re-add the onClick events each time you replace the HTML or use the onClick inline like
onclick="something()"
.You also have a syntax error, you're using a lowercase F in
ajaxfunction('4',""+s);
, whereas the function is namedajaxFunction
.