gpt4 book ai didi

javascript - 检查innerHTML是否为空

转载 作者:行者123 更新时间:2023-11-28 21:05:27 24 4
gpt4 key购买 nike

我对 onkeyup 事件进行了 ajax 调用,该调用检查给定的电子邮件地址是否在数据库中。我的问题是,我找不到正确的解决方案来检查 ajax 响应是否为空,如果是,我需要禁用按钮。

这是 ajax.php 输出:

$sql = "// the query";

$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);

if($num_rows>0){
echo "<i>Yepp, this is taken</i>";
}

这是用于 ajax 调用和按钮禁用和启用功能的 JavaScript(仅当电子邮件地址有效时才调用 ajax):

function checkEmail(str){

email = document.getElementById('user-email').value
AtPos = email.indexOf("@")
StopPos = email.lastIndexOf(".")

if (AtPos == -1 || StopPos == -1) {
document.getElementById('button-reg').disabled=true;
}else{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status == 200){
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax.php?q="+str,true);
xmlhttp.send();
}
var htmlstring = document.getElementById("txtHint").innerHTML;

htmlstring = htmlstring.replace(/^\s+/,'');

if(htmlstring==""){
document.getElementById('button-reg').disabled=false;
}else{
document.getElementById('button-reg').disabled=true;
}
}

正如你所看到的,我尝试消除空格,但没有运气...空响应后按钮仍然没有禁用...

有人可以帮我吗?

最佳答案

使用此功能..

function checkEmail(str){

email = document.getElementById('user-email').value
AtPos = email.indexOf("@")
StopPos = email.lastIndexOf(".")

if (AtPos == -1 || StopPos == -1) {
document.getElementById('button-reg').disabled=true;
}else{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status == 200){
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

htmlstring = xmlhttp.responseText.replace(/^\s+/,'');
if(htmlstring==""){
document.getElementById('button-reg').disabled=false;
}else{
document.getElementById('button-reg').disabled=true;
}
}
}
xmlhttp.open("GET","ajax.php?q="+str,true);
xmlhttp.send();
}
}

希望这有帮助..

关于javascript - 检查innerHTML是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9994422/

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