简体   繁体   English

为什么这小部分的jQuery会弄乱我的其余JavaScript?

[英]Why is this tiny bit of jQuery messing up the rest of my JavaScript?

I have an area on my page that has tabular data. 我的页面上有一个包含表格数据的区域。 I am attempting to add in-line editing of the values in each row. 我试图在每一行中添加值的嵌入式编辑。 I have tried jqGrid, but it will not work for my needs. 我已经尝试过jqGrid,但是它不能满足我的需求。 So, I am attempting a "home brew" version of in-line editing. 因此,我正在尝试在线编辑的“家庭酿造”版本。 My table is simple enough: 我的表很简单:

<table>
    <tbody>
        <tr id="row0" onClick="changeMe(this);">
            <td>
                some value
            </td>
            <td>
                some other value
            </td>
            <td>
                ...
            </td>
        </tr>
        <tr id="rowN" onClick="changeMe(this);">
            <td>
                ...
            </td>
        </tr>
    </tbody>
</table>

When a user clicks anywhere on the TR, I want my JavaScript to change all static text to the proper form inputs. 当用户单击TR上的任意位置时,我希望我的JavaScript将所有静态文本更改为适当的形式输入。 The first two elements of the form are select boxes. 表单的前两个元素是选择框。 The first box needs to be filled in before the second box's options are available. 在第二个框的选项可用之前,需要先填写第一个框。 Here is part of the JavaScript I am using to accomplish this: 这是我用来完成此操作的JavaScript的一部分:

function changeMe(row){
    var serviceArray = ["option_1-1","option_1-2","option_1-3","option_1-4","option_1-5"]
    for(var i=0; i<2; i++){
        if(row.children[i].children.length == 0){
            var selectBox = document.createElement("select");
            var.setAttribute("id",serviceArray[i]);
            var.setAttribute("name",serviceArray[i]);

            switch(i){
                case 0:
                    $.getJSON("./dataGetter.php?qid=" + Math.random(),function(data){
                        $.each(data,function(key,value){
                            var option = document.createElement("option");
                            var innerValue = document.createTextNode(value);
                            option.appendChild(innerValue);
                            option.setAttribute("value",value);
                            selectBox.appendChild(option);
                        });
                    });
                    selectBox.setAttribute("onChange","someFunction(this.value)");
                    break;
                case 1:
                    selectBox.setAttribute("onChange","someOtherFunction(this.value)");
                    var option = document.createElement("option");
                    option.setAttribute("selected","selected");
                    option.setAttribute("disabled","disabled");
                    var textNode = document.createTextNode("\u2190 choose that first");
                    option.appendChild(textNode);
                    selectBox.appendChild(option);
                    break;
            }
            row.children[i].innerHTML = null;
            row.children[i].appendChild(selectBox);
        }
    }
}

This is where my problem comes in. when a user clicks the TR, child 0 of the TR, which is a TD with text, should be converted into the appropriate select box, filled with the appropriate data from the database. 这就是我的问题所在。当用户单击TR时,TR的子0(带有文本的TD)应转换为适当的选择框,并填充数据库中的适当数据。 This, however, is not happening. 但是,这没有发生。 What is happening, is that child 1 of the TR (ie, the second TD) is the one being converted instead. 发生的情况是,TR的子项1(即第二个TD)已被转换。

Just as a test, I added a new test array to the function: 就像测试一样,我向函数添加了一个新的测试数组:

var testArray = new Array("option_2-1","option_2-2","option_2-3","option_2-4","option_2-5");

and changed my JavaScript to the following: 并将我的JavaScript更改为以下内容:

function changeMe(row){
    var serviceArray = ["option_1-1","option_1-2","option_1-3","option_1-4","option_1-5"];
    var testArray = new Array("option_2-1","option_2-2","option_2-3","option_2-4","option_2-5");
    for(var i=0; i<2; i++){
        if(row.children[i].children.length == 0){
            var selectBox = document.createElement("select");
            var.setAttribute("id",serviceArray[i]);
            var.setAttribute("name",serviceArray[i]);

            switch(i){
                case 0:
                    $.each(testArray,function(key,value){
                        var option = document.createElement("option");
                        var innerValue = document.createTextNode(value);
                        option.appendChild(innerValue);
                        option.setAttribute("value",value);
                        selectBox.appendChild(option);
                    });
                    selectBox.setAttribute("onChange","someFunction(this.value)");
                    break;
                case 1:
                    selectBox.setAttribute("onChange","someOtherFunction(this.value)");
                    var option = document.createElement("option");
                    option.setAttribute("selected","selected");
                    option.setAttribute("disabled","disabled");
                    var textNode = document.createTextNode("\u2190 choose that first");
                    option.appendChild(textNode);
                    selectBox.appendChild(option);
                    break;
            }
            row.children[i].innerHTML = null;
            row.children[i].appendChild(selectBox);
        }
    }
}

When I make this change, the first TD (child 0) of the TR is changed as it should be. 当我进行此更改时,TR的第一个TD(子0)将按原样进行更改。

When I go directly to my dataGetter.php page in the browser, I get the correct data: 当我直接在浏览器中进入dataGetter.php页面时,将获得正确的数据:

["option_2-1","option_2-2","option_2-3","option_2-4","option_2-5"]

I know this is the correct data, because I put an "alert();" 我知道这是正确的数据,因为我输入了“ alert();”。 just after the "$.each" line, and the proper data was shown in the alert pop-up. 紧接在“ $ .each”行之后,警报弹出窗口中显示了正确的数据。

So, I'm not sure what is happening here. 所以,我不确定这里发生了什么。 It looks like the one line 看起来像一条线

$.getJSON("./dataGetter.php?qid=" + Math.random(),function(data){
}

is messing up my switch statement, and populating the second TD (child 1) with child 0 and child 1 data both. 正在弄乱我的switch语句,并用子0和子1数据填充第二个TD(子1)。 It's almost like the "break" in "case 0" is being removed, and all the logic is falling through to the second case. 这几乎就像删除了“情况0”中的“中断”一样,所有逻辑都进入了第二种情况。

UPDATE: If I add the line "alert(i);" 更新:如果我添加“ alert(i);”行 after my for loop, then everything works as it should ... except for the annoying pop-up. 在我的for循环之后,然后一切都会正常进行……除了烦人的弹出窗口。

For all those that are interested, I have figured out the problem. 对于所有感兴趣的人,我都已经解决了这个问题。 It appears that my switch statement was completing before the AJAX return came back. 看来我的switch语句在AJAX返回返回之前已经完成。 So, I set "async" to "false" and everything worked perfectly. 因此,我将“异步”设置为“假”,并且一切正常。

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

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