if语句中的.ajax只能间歇性地触发jquery

时间:2017-06-23 15:39:29

标签: javascript php jquery html ajax

好的,所以我继承了一堆代码,我需要从一个非常旧版本的原型升级到嵌入一个使用php和smarty模板(ugh)的旧网站的jquery 3.2.1。

登录的用户最多可以生成五个案例编号;它们存储在会话变量和数据库中。这些是一次性的数字 - 如果它们没有被使用,它们最终会被回收。当用户登录时,他们最多只能生成5个数字。

我已将旧的原型代码翻译成jquery等价物,并且它只是间歇性地发射。检索新案例编号的函数调用绑定到脚本的document.ready部分中加载的按钮。

编辑:包括整个shebang

        function GetNewCaseNumber() {
            if (CurrentNumber <= 5) {
                if (CurrentNumber == 1) {
                    NewNumber = true;
                    $('#getnumber').val("Get Additional Case Number");
                } else {
                    NewNumber = confirm("Are you sure you want another Case Number?");
                }
                console.log("CurrentNumber: %s, NewNumber: ", CurrentNumber, NewNumber);
                // Show Confirm Dialog
                if (NewNumber == true) {
                    // Get another case number
                    // this fires sometimes, sometimes doesn't.  why?
                    console.log("Div tag to update= %s", "CaseNumber" + CurrentNumber);
                    console.log("Current html content value= %s",$('#CaseNumber' + CurrentNumber).html());
                    if (($.trim($('#CaseNumber' + CurrentNumber).html()) == '') || ($('#CaseNumber' + CurrentNumber).val() == null))                        {
                        $.ajax({
                            url: ("../ajax.php?event=ObtainCaseNumber&number=" + CurrentNumber),
                            async: true,
                            type: "GET",
                            dataType: "json",
                            success: function (server_response) {
                                console.log("Current Case Number %s", server_response);
                                $('#CaseNumber' + CurrentNumber).text(server_response);
                                $('#CaseNumber' + CurrentNumber).fadeIn("slow");
                                CurrentNumber++;
                            },
                            error: function (server_response) {
                                alert("There has been an ajax error: " + server_response);
                            }
                        });
                        $('#CaseNumber' + CurrentNumber).fadeIn("slow");
                        /* there are at least 2 casenumbers, show "these numbers" */
                        $('#thisnumber').html("these numbers");
                    }
                }
                // Hide Additional Buttons if this is the last one
                if (CurrentNumber >= 6) {
                    $("#getnumber").fadeOut("slow");
                }
            } else {
                alert("You cannot generate anymore Case Number's at this time.");
                $("#getnumber").fadeOut("slow");
            }
        }

我已编辑该功能以移动CurrentNumber增加的位置。它现在似乎完美无缺。感谢大家的快速帮助和回复!

        function CheckCurrentCaseNumbers()
        {
        // Check what case numbers we already have
            $(".caseList").each(function() {
            if ($('#CaseNumber' + CurrentNumber).html() != "")
            {
                console.log("CCCN CurrentCaseNumber: %s", 'CaseNumber' + CurrentNumber);
                $('#CaseNumber' + CurrentNumber).fadeIn("slow");
                CurrentNumber++;
                console.log("CCCN CurrentNumber: %s", CurrentNumber);
            }
            });
            /* If there's at least 2 casenumbers, show "these numbers" */
            if ($("#CaseNumber2").html != "")
            {
                $("#thisnumber").html("these numbers");
            }
            if ($("#CaseNumber1").html())
            {
                $("#getnumber").val("Get Additional Case Number");
            }
            $("#Loading").fadeOut("3000");
        }

这是由document.ready标签中绑定的按钮上的on click事件触发的,而NewNumber == true是警报确认。它应该更新这些div:

                <div id="CaseNumber1" class="caseList" style="display:none;">{$casenumbers.1}</div>
                <div id="CaseNumber2" class="caseList" style="display:none;">{$casenumbers.2}</div>
                <div id="CaseNumber3" class="caseList" style="display:none;">{$casenumbers.3}</div>
                <div id="CaseNumber4" class="caseList" style="display:none;">{$casenumbers.4}</div>
                <div id="CaseNumber5" class="caseList" style="display:none;">{$casenumbers.5}</div>

新的控制台日志:

...初始化

CurrentNumber: 1
(index):23 generating case number
(index):24 Current Number: 1
(index):132 CurrentNumber: 1, NewNumber:  true
(index):137 Div tag to update= CaseNumber1
(index):138 Current html content value= 
(index):146 Current Case Number 11481593
(index):23 generating case number
(index):24 Current Number: 2
(index):132 CurrentNumber: 2, NewNumber:  true
(index):137 Div tag to update= CaseNumber2
(index):138 Current html content value= 
(index):146 Current Case Number 11481594
(index):23 generating case number
(index):24 Current Number: 3
(index):132 CurrentNumber: 3, NewNumber:  true
(index):137 Div tag to update= CaseNumber3
(index):138 Current html content value= 
(index):146 Current Case Number 11481595
(index):23 generating case number
(index):24 Current Number: 4
(index):132 CurrentNumber: 4, NewNumber:  true
(index):137 Div tag to update= CaseNumber4
(index):138 Current html content value= 
(index):146 Current Case Number 11481596
(index):23 generating case number
(index):24 Current Number: 5
(index):132 CurrentNumber: 5, NewNumber:  true
(index):137 Div tag to update= CaseNumber5
(index):138 Current html content value= 
(index):146 Current Case Number 11481597
(index):23 generating case number
(index):24 Current Number: 6

感谢您的帮助!

朱莉

这是我的文件准备好了:

    var CurrentNumber = 1;
    $(document).ready(function() {
    $('#getnumber').val("Obtain Case Number");
    $("#Loading").fadeIn("slow");
    /* console logging for debugging -- will be removed */
    console.log("Init...");
    console.log("CurrentNumber: %s", CurrentNumber);
    CheckCurrentCaseNumbers();
    $('#getnumber').on( "click", function() 
        {
            console.log( "generating case number" );
            console.log( "Current Number: %s", CurrentNumber );
            GetNewCaseNumber();
        });
    });   

触发此按钮的按钮:

                <form>
                    <input type="button" value="Obtain Case Number" id="getnumber" />
                </form>

CurrentNumber需要是全局的(我知道jQuery确实没有全局变量)所以我首先要初始化它

1 个答案:

答案 0 :(得分:0)

根据你的控制台输出,CaseNumber3元素的html内容是“11481573”而不是“”,因此IF语句无法运行。

查看突出显示的行:

(index):19 CurrentNumber: 1 
(index):176 CCCN CurrentCaseNumber: CaseNumber1 
(index):179 CCCN CurrentNumber: 2 
(index):23 generating case number 
(index):24 Current Number: 2 
(index):132 CurrentNumber: 2, NewNumber: true 
(index):137 Current html content value= 
(index):147 Current Case Number 11481573 
(index):23 generating case number 
(index):24 Current Number: 3 
(index):132 CurrentNumber: 3, NewNumber: true 
(index):137 Current html content value= 11481573 // <-- HERE
(index):23 generating case number 
(index):24 Current Number: 3 
(index):132 CurrentNumber: 3, NewNumber: true 
(index):137 Current html content value= 11481573 
(index):23 generating case number 
(index):24 Current Number: 3 
(index):132 CurrentNumber: 3, NewNumber: true 
(index):137 Current html content value= 11481573 
(index):23 generating case number 
(index):24 Current Number: 3 
(index):132 CurrentNumber: 3, NewNumber: true 
(index):137 Current html content value= 11481573 
(index):23 generating case number 
(index):24 Current Number: 3 
(index):132 CurrentNumber: 3, NewNumber: true 
(index):137 Current html content value= 11481573