- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先:我不想使用 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 区分大小写。
此外,您需要将regit
的onclick
函数的声明移动到handleServerResponse()
函数内部,在thelement.innerHTML=xmlhttp.responseText;
,因为在调用这行之前,没有id="regit"
的元素,所以函数的绑定(bind)不起作用.
此外,您应该使用 ;
终止声明为 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);
}
}
}
关于php - PHP + Ajax innerHTML 后可点击的 JS 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8606052/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!