根据Ajax响应禁用“提交”按钮 [英] Disable Submit Button Based on Ajax response

查看:143
本文介绍了根据Ajax响应禁用“提交”按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总之,我想做的是基于ajax调用禁用提交按钮,该按钮正在检查我的数据库以查看数据是否在特定日期存在。

Ok in summary what I am trying to do is disable a submit button based on an ajax call which is checking my database to see if data exists under a certain date.

目前,我已经完成了所有上述工作,但是在实际上禁用了提交按钮时,我遇到了最后的障碍。现在,我的ajax调用的响应在页面上的div中显示为文本,我可以通过更改以下代码来进行操作,如果响应与我的代码所检查的响应不匹配但无法使其正常运行,请禁用该按钮匹配(或不匹配)。我想我的问题是我要检查的内容和来自ajax调用的实际响应是不同的。任何帮助将不胜感激。

At present I have got all of the above working however I am falling at the final hurdle in actually disabling the submit button. At presnt I have the response of my ajax call being displayed as text in a div on my page and I can by altering the code below, disable the button if the response does not match that being checked by my code but cannot get it working correctly be matching(or not the response). I guess my problem is that what I am checking and what the actual response from the ajax call is are different. Any help would be much appreciated.

我进行ajax调用的页面

<script type="text/javascript">
function showHint(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  document.getElementById("txtHint").style.border="0px";
  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;
    document.getElementById("txtHint").style.border="1px solid #A5ACB2";
    if (xmlhttp.responseText == 'WARNING!! - PREVIOUS RECORDS FOUND') document.getElementById('FRINSUB').disabled = true;
    else
    document.getElementById('FRINSUB').disabled = false;
}
 }
xmlhttp.open("GET","checkdate.php?q="+str,true);
xmlhttp.send();
}
</script>

我非常基本的ajax代码

<?php

$cdrf=trim($_GET["q"]);
list($d, $m, $y) = explode('/', $cdrf);
$mk=mktime(0, 0, 0, $m, $d, $y);
$cdrfq=strftime('%Y-%m-%d',$mk);

$q=mysql_real_escape_string($cdrfq);
$isdate = false;

$con = mysql_connect("server","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("jbsrint", $con);
$query = "SELECT * FROM fuelrecords WHERE FR_WE='$q'";
$res = mysql_query($query);
if(mysql_num_rows($res) > 0){
$response = "WARNING!! - PREVIOUS RECORDS FOUND";
}else{
$response = "NO RECORD FOUND";
}
echo $response;
?>


推荐答案

好,我知道了。无论出于什么原因(仍然无法从哪里找到),我都会在返回的ajax代码上换行,尽管它只是空格,但if语句与我要求它检查的短语不符。

Ok I figured it out. It transpires for whatever reason (Still cant find out where from) I was getting a newline on the returned ajax code and so albeit it was only blank space the if statement could not match the phrase I had asked it to check.

使用$ .trim(位于 jQuery'If'语句字符串比较无法正常工作)我删除了空格,并全部激活了。

By using $.trim (found in jQuery 'If' statement string comparison not working) I removed the blank space and it all fired into life.

新代码现在看起来像这样。

The new code now looks like this.

if ($.trim(xmlhttp.responseText) == "WARNING!! - PREVIOUS RECORDS FOUND") document.getElementById('FRINSUB').disabled = true;

这篇关于根据Ajax响应禁用“提交”按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆