1

这是我的 JavaScript 代码

  function validatenewcat(){
        var category = document.getElementById("cat").value;
        if(category==""){
            setTimeout(document.getElementById("validate").innerHTML="!PLz Enter The Category Name", 2000);
            return false;
        }
    else{
        var url="catnamecheck.do?id="+category;
         xmlhttp.open("post", url,true);
         xmlhttp.onreadystatechange=(function (callback){
             return function (){
          if(xmlhttp.readyState==4){      
                 if(xmlhttp.status==200){   
                    var temp = xmlhttp.responseText;
                    obj = JSON.parse(temp);
                  if(obj.catgoryname==category){
                      alert("inside flase");
                     var validate = document.getElementById("validate");
                     validate.innerHTML="!PLz Enter The Unique Category Name";
                         callback(false); 
                     }
                 if(obj.catgoryname!=category){
                    alert("inside true");
                         callback(true);
                     }
                  }
              }
           }
         })(myCallback);
         xmlhttp.send(null);
        }
    }

谁能帮我这个函数在这里没有返回真我正在使用ajax从javacode检查唯一的类别名称这是我的回调函数

  function myCallback(returnedValue) {
        if (returnedValue) {
            alert("entered callback function");
            // true returned. Validation successful. Do whatever you want
            // here in this case.
        }
        else {
            // false returned. Validation failed. Handle the "false" scenario here
            alert("entered callback function");
        }
    }

这是我的jsp代码

<form name="frm" action="createnewcatgoryBean.jsp"  method="post" onsubmit="return validatenewcat()">
<table style="padding: 5px 0px 0px 8px;">
<tr>
<th colspan="2">
<div style="width: width:271px; color:red;" id="validate"></div>
</th>
</tr>
<tr>
<th>Category Name<span>:</span></th><td><input id="cat"  onblur="return validatenewcat()" type="text" name="category">
</td>
</tr>
<tr>
<th>Quotations form<span>:</span></th><td><input type="checkbox" name="quotations"></td>
</tr>
<tr>
<th>Agreement form<span>:</span></th><td><input type="checkbox" name="agreement"></td>
</tr>
<tr>
<th>Payment form<span>:</span></th><td><input type="checkbox" name="payment"></td>
</tr>
<tr>
<th>ETI<span>:</span></th><td><input type="checkbox" name="eti"></td>
</tr>
<tr>
<td colspan="2" style="float:right; padding-top:15px">
<input type="submit" value="Submit"  style="width: 60px;">
</td>
</tr>
</table>
</form>

谁能告诉我如何返回成功或失败表单回调函数这是我编辑的代码


这是我的 JavaScript 代码

function validatenewcat(){
        var category = document.getElementById("cat").value;
        if(category==""){
            setTimeout(document.getElementById("validate").innerHTML="!PLz Enter The Category Name", 2000);
            return false;
                 }
    else{
        var url="catnamecheck.do?id="+category;
         xmlhttp.open("post", url,true);
         xmlhttp.send(null);
         xmlhttp.onreadystatechange= function (){
          if(xmlhttp.readyState==4){      
                 if(xmlhttp.status==200){   
                    var temp = xmlhttp.responseText;
                    obj = JSON.parse(temp);
                   var unique = (obj.catgoryname != category);
                  if(unique ){
                      document.getElementById("newcatform").submit();
                             }
                  else{
                     var validate = document.getElementById("validate");
                     validate.innerHTML="!PLz Enter The Unique Category Name";
                       }
                  }
              }
           };
           return false;    
        }

这是我的jsp代码

<form id="newcatform" name="frm" action="createnewcatgoryBean.jsp"  method="post">
<table style="padding: 5px 0px 0px 8px;">
<tr>
<th colspan="2">
<div style="width: width:271px; color:red;" id="validate"></div>
</th>
</tr>
<tr>
<th>Category Name<span>:</span></th><td><input id="cat"  onblur="return validatenewcat()" type="text" name="category">
</td>
</tr>
<tr>
<th>Quotations form<span>:</span></th><td><input type="checkbox" name="quotations"></td>
</tr>
<tr>
<th>Agreement form<span>:</span></th><td><input type="checkbox" name="agreement"></td>
</tr>
<tr>
<th>Payment form<span>:</span></th><td><input type="checkbox" name="payment"></td>
</tr>
<tr>
<th>ETI<span>:</span></th><td><input type="checkbox" name="eti"></td>
</tr>
<tr>
<td colspan="2" style="float:right; padding-top:15px">
<input type="button" value="Submit" onclick="validatenewcat()"  style="width: 60px;">
</td>
</tr>
</table>
</form>
</div>

        }
4

1 回答 1

3

其中的代码onreadystatechange称为“事件处理程序”,只有在您的 POST 操作完成后才会执行。这是您的validatenewcat函数在运行时实际执行的操作:

var category = document.getElementById("cat").value;
var url = "catnamecheck.do?id=" + category;
xmlhttp.open("post", url, true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = (code to be called later)
return false; // <-- Always return false

您需要在 onreadystatechange 函数内移动需要TRUE / FALSE结果的代码,以便在 HTTP 操作完成后执行:

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            var temp = xmlhttp.responseText;
            obj = JSON.parse(temp);
            var unique = (obj.catgoryname != category);

            if (unique) {
                // Submit the form to the server
                document.getElementById("newcatform").submit();
            } else {
                document.getElementById("validate").innerHTML = "!PLz Enter The Unique Category Name";
            }
        }
    }
}

在您的 JSP 中,您可以在提交表单之前使用此函数来验证您的类别:

<form id="newcatform" name="frm" action="createnewcatgoryBean.jsp" method="post">
...
<input type="button" value="Submit" onclick="validatenewcat()">
</form>

显然,一些重命名也是明智的。例如,“validatenewcat()”可以变成“validateandsend()”。

最后,为了避免时间问题,您应该在发送请求之前设置回调函数,以便在响应返回时它已经存在:

xmlhttp.open("post", url, true);
xmlhttp.onreadystatechange = function () {
    // ... callback code here ...
}
xmlhttp.send(null); // <- Move under onreadystatechange callback
于 2013-09-03T09:20:35.097 回答