Home > Backend Development > PHP Tutorial > 请问个AJAX有关问题

请问个AJAX有关问题

WBOY
Release: 2016-06-13 13:30:25
Original
1021 people have browsed it

请教个AJAX问题

JScript code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
     function nums(){
        var n = document.natform.num.value;
            
        var url="index.php?c=nat&a=natid&id="+n+"";
        if(n == ''){
            document.getElementById('num').style.display='block';
            document.getElementById('num').innerHTML='编号不能为空';
            document.getElementById('num').style.color='red';
            return false;
        }else{
            xmlHttpRequest = createXmlHttpRequest();
            xmlHttpRequest.onreadystatechange = handle;
            xmlHttpRequest.open("GET",url,true);
            xmlHttpRequest.send(null);
                        //我的问题就是 在这个else里怎么接收到 handle()的返回值(true  false)
        }
    
    }
    function handle(){
        if(xmlHttpRequest.readyState == 4){    
            var re = xmlHttpRequest.responseText;
            if(re){
                document.getElementById('num').style.display='block';
                document.getElementById('num').innerHTML='编号已存在';
                document.getElementById('num').style.color='red';
                return false;
            }else{
                document.getElementById('num').style.display='block';
                document.getElementById('num').innerHTML='可以使用';
                document.getElementById('num').style.color='green';
                return true;
            }
            
        }    
    }

Copy after login


以上是一个ajax 判断 id 是否存在的, 如果不存在,可以提交,如果存在提示已存在,不能提交
JScript code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
    function saves(){
        if(nums()){
            document.natform.action="index.php?c=nat&a=addnatpost";
                    document.natform.submit();
        }
    }

Copy after login

但是现在问题是 num() 接收不到 handle() 的返回值, 所以已存在的时候 num() 状态是未定义的, 请教下这块怎么改

------解决方案--------------------
JScript code

 var re = xmlHttpRequest.responseText;    //这里的re值是多少?alert看看。
//如果没有值,说明是后端接收处理的问题
<br><font color="#e78608">------解决方案--------------------</font><br>
ajax 是异步通讯方式,所以你不可能从 num() 得到正确的返回值<br>你所需要的提交动作应在 handle 中完成 <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template