简体   繁体   English

从if块成功调用函数

[英]Calling a function from if block in success

Hello fellow programmers. 各位程序员大家好。 I am newbie to jquery ajax. 我是jquery ajax的新手。 How do i call function checkreturn() from if block or is it possible to access msg outside the success if yes then please let me know how. 我如何从if块调用函数checkreturn()或是否可以在成功之外访问msg(如果是),那么请告诉我。 I need it because only if condition proves true i have to enable the subsequent textbox. 我需要它,因为只有在条件成立的情况下,我才必须启用后续的文本框。 Here is my code.Thanks in advance for your time and reply.Rajesh. 这是我的代码。谢谢您的时间和答复。Rajesh。

<script type="text/javascript" >    

        function checkreturn()        {   

            document.getElementById("txtAns").removeAtrribute("disabled");                     
        }
        function cQtn(e){             
        var uname= $("#<%=Username.ClientID%>").val();
        var sq=$("#<%=SecQuest.ClientID%>");
        var sqtn = $("#<%=SecQuest.ClientID%> option:selected").text();
        var sans=$("#txtAns");      
        var msgbox = $("#Dstatus");
          $.ajax({
                    type: "POST",                    
                    url: "forgotpassword.aspx/CheckValidSQtn",
                    data: "{'uname':'"+uname+"','args':'"+sqtn+"'}", 
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {                                                                                                             
                         if (msg.d == 'Available') {                            
                            sq.removeClass("notavailablecss");
                            sq.addClass("availablecss");
                            msgbox.html('<img src="proj_mages/a.png"> <font color="Green"> Valid </font>');
                            //how do i call from here??                                                                                                              
                           }
                        else {                            
                            sq.removeClass("availablecss");
                            sq.addClass("notavailablecss");
                            msgbox.html(msg.d);                           
                        }                        
                    }                                                 
                });                                     
      }     

    </script>

You have a typo in your checkreturn function. 您的checkreturn函数中有一个错字。 You want to use removeAttribute , instead of removeAtrribute (double t ,not double r ). 您要使用removeAttribute ,而不是removeAtrribute (double t ,而不是double r )。

Also, you can use jQuery functions: 另外,您可以使用jQuery函数:

function checkreturn(){   
    $('#txtAns').prop('disabled',false);                
}

, instead of native DOM functions ( document.getElementById , setAttribute ): ,而不是本机DOM函数( document.getElementByIdsetAttribute ):

not sure what why the normal way is not working but you could try forcing what the browser is supposed to do, 不知道为什么正常方式不起作用,但是您可以尝试强制浏览器执行该操作,

function checkreturn(){   
     document.getElementById("txtAns").removeAtrribute("disabled");                     
}

Becomes 成为

window.checkreturn = function(){   
     document.getElementById("txtAns").removeAtrribute("disabled");                     
}

Then try calling via window.checkreturn(); 然后尝试通过window.checkreturn();调用 or checkreturn(); checkreturn(); you can also so try this the other way arround so you can leave your function and try calling window.checkreturn(); 您还可以尝试以另一种方式尝试此操作,以便离开函数并尝试调用window.checkreturn();

If none of these are working it would say your function is not entering the window(Global) scope for your page use Firebug or Inspector and try to all checkreturn(); 如果这些都不起作用,则说明您的函数未进入页面的window(Global)范围,请使用Firebug或Inspector并尝试所有checkreturn(); see what exception you get back, 看你得到什么异常,

if you get a not found your not showing us some thing in your code maybe a closure or some thing 如果发现未找到,则未在代码中向我们显示某些内容,例如关闭或某些内容

I'll look into it further but try setting async for the ajax call to false : 我将进一步研究它,但尝试将ajax调用的async设置为false

function checkreturn() {

    document.getElementById("txtAns").removeAtrribute("disabled");

}

function cQtn(e) {

    var uname= $("#<%=Username.ClientID%>").val(),
        sq=$("#<%=SecQuest.ClientID%>"),
        sqtn = $("#<%=SecQuest.ClientID%> option:selected").text(),
        sans=$("#txtAns"),
        msgbox = $("#Dstatus");

    $.ajax( {
        async: false,
        type: "POST",
        url: "forgotpassword.aspx/CheckValidSQtn",
        data: "{'uname':'"+uname+"','args':'"+sqtn+"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg.d == 'Available') {
                sq.removeClass("notavailablecss");
                sq.addClass("availablecss");
                msgbox.html('<img src="proj_mages/a.png"> <font color="Green"> Valid </font>');
                //how do i call from here??
            } else {
                sq.removeClass("availablecss");
                sq.addClass("notavailablecss");
                msgbox.html(msg.d);
            }
        }
    } );

}

Now, when the ajax call is made, the rest of the script will wait til it completes instead of how everything continues when async is true . 现在,当进行ajax调用时,脚本的其余部分将等待直到完成为止,而不是等到asynctrue时一切如何继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM