简体   繁体   English

如何根据AJAX调用的响应突破javascript循环?

[英]How do I break out of javascript loop based on response from AJAX call?

My topic has been covered extensively here. 我的主题在这里已被广泛讨论。 I have searched alot and tried alot of code but have not found anything that helps me solve my specific situation. 我已经搜索了很多代码,并尝试了很多代码,但没有找到任何可以帮助我解决特定情况的东西。 So I would like to pose this question to the community. 因此,我想向社区提出这个问题。

I am building a page with 12 form fields where I loop through the fields and make an AJAX call to test the value against a database. 我正在构建一个包含12个表单字段的页面,在其中循环浏览这些字段并进行AJAX调用以针对数据库测试值。 My issue is that I need to stop my loop and get user input based on a particular response from my AJAX call. 我的问题是我需要停止循环并根据AJAX调用的特定响应获取用户输入。 So far I have been unable to stop my loop until it reaches the end. 到目前为止,我一直无法停止循环,直到循环结束。

Here is my form: 这是我的表格:

<form name="claimForm">
<table>
<tr>
<td> <input type="text" id="icdcode1"></td>
<td> <input type="text" id="icdcode2"></td>
<td> <input type="text" id="icdcode3"></td>
<td> <input type="text" id="icdcode4"></td>

</tr>

<tr>
<td> <input type="text" id="icdcode5"></td>
<td> <input type="text" id="icdcode6"></td>
<td> <input type="text" id="icdcode7"></td>
<td> <input type="text" id="icdcode8"></td>

</tr>

<tr>
<td><input type="text" id="icdcode9"> </td>
<td> <input type="text" id="icdcode10"></td>
<td> <input type="text" id="icdcode11"></td>
<td> <input type="text" id="icdcode12"></td>

</tr>

<tr> <td colspan="4"> <input type="button" value="Check IDC Code" onClick="javascript:claimValidate();"></td></tr>
</table>


</form>

<div id="pageresponse"></span>

Here is my claimValidate function: 这是我的ClaimValidate函数:

function claimValidate() {

var isFound = false; 
for (i = 1; i <= 12; i++) {

    var icdcode = 'icdcode'+i;

    if (!isFound) {
        if(document.getElementById(icdcode)) {
            var retrieved = document.getElementById(icdcode).value;

                if (retrieved != "") {

                       if (ajaxCheck(retrieved)) {

                        }


                } // end if retrieved

        } // end if document.getElementbyId
    } // end if !isFound    



} // end claimValidate

And here is my ajaxCheck function 这是我的ajaxCheck功能

function ajaxCheck(icdcode) {

var xmlhttp = new XMLHttpRequest();    

 xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            //alert(isFound)
            resp = xmlhttp.responseText;

            if (resp == icdcode) {
                return true;
            } else if (resp == "Search Failed") {
                alert("You must enter a valid issue code.")
                return false;
            }  // end if resp

        if ((resp !== "Search Failed") && (resp.length > 0)) { 
                document.getElementById("pageresponse").innerHTML = resp;
                return false;
            }

        } // end readystate

    } // end onreadystatechange

                xmlhttp.open("GET", "validate_idc_code.asp?q=" + icdcode, true);
                xmlhttp.send();

} // end ajaxCheck

My intent is to break out of the loop and stop if it gets to the final if statement in ajaxCheck but it is not and I'm not sure why. 我的目的是打破循环,如果到达ajaxCheck中的最终if语句,则停止,但不是,并且我不确定为什么。 I showed my code to a co-worker and he seemed to think that ajaxCheck would not actually return anything because it was being called in a separate function. 我向同事展示了代码,他似乎认为ajaxCheck实际上不会返回任何东西,因为它是在单独的函数中调用的。 He suggested passing all my form fields as an array to the ASP page I have doing the database check, but I'm not sure that is the best solution. 他建议将我所有的表单字段作为数组传递给我已经进行数据库检查的ASP页,但是我不确定这是最好的解决方案。

I would be grateful for any guidance the experts out there could provide for me. 我很感谢专家为我提供的任何指导。

Thank you! 谢谢!

-- Chris - 克里斯

Well you can do it by using a global var indicating that you want your loop to continue or stop. 好吧,您可以通过使用全局变量var来完成此操作,该变量指示您希望循环继续或停止。 then in claimValidate you would put something like 然后在claimValidate中,您将

if (!loopActive) return;

But this is a bad practice. 但这是一个不好的做法。 The best is restructuring like @MarcB and @JamesThorpe had adviced. 最好的方法是像@MarcB和@JamesThorpe建议的那样进行重组。

I only answered with this ideia, because I understand that in order to learn anything you have to progress initially in not the best way and then progressively understand why that is not the best practice. 我只回答了这种想法,因为我知道要学习任何东西,您必须先以最佳方式进步,然后逐步理解为什么这不是最佳做法。

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

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