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

为什么这个小小的jQuery会把我的JavaScript的其余部分弄乱?,javascript,jquery,Javascript,Jquery,我的页面上有一个区域有表格数据。我正在尝试添加对每行中的值的在线编辑。我试过jqGrid,但它不能满足我的需要。因此,我正在尝试在线编辑的“自制”版本。我的桌子很简单: <table> <tbody> <tr id="row0" onClick="changeMe(this);"> <td> some value </td>

我的页面上有一个区域有表格数据。我正在尝试添加对每行中的值的在线编辑。我试过jqGrid,但它不能满足我的需要。因此,我正在尝试在线编辑的“自制”版本。我的桌子很简单:

<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>
并将我的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);
        }
    }
}
我知道这是正确的数据,因为我在“$.each”行后面放了一个“alert();”,并且在警报弹出窗口中显示了正确的数据

所以,我不确定这里发生了什么。它看起来像一条线

$.getJSON("./dataGetter.php?qid=" + Math.random(),function(data){
}
正在弄乱我的switch语句,并用子0和子1数据填充第二个TD(子1)。这几乎就像“案例0”中的“中断”被删除,所有的逻辑都进入了第二个案例


更新:如果我在for循环之后添加行“alert(I);”,那么一切都会正常工作。。。除了恼人的弹出窗口。

对于所有感兴趣的人,我已经解决了问题。我的switch语句似乎在AJAX返回之前就完成了。因此,我将“async”设置为“false”,所有操作都很正常。

这一行看起来非常错误
selectBox.setAttribute(“onChange”,“someFunction(This.value)”)
。您应该使用
addEventListener
添加事件,它可以工作。。。某种程度上。这行有12个单元格,所有的输入、复选框和第二个选择输入都被正确地更改了,只是第一个选择框被弄乱了。
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);
        }
    }
}
["option_2-1","option_2-2","option_2-3","option_2-4","option_2-5"]
$.getJSON("./dataGetter.php?qid=" + Math.random(),function(data){
}