0

在 Java EE 应用程序上,我有以下点击功能

function calculateK() {


    var matD = "" + $('.dr1c1').val() + "__" + $('.dr1c2').val() + "__" + $('.dr1c3').val()
            + "__" + $('.dr2c1').val() + "__" + $('.dr2c2').val() + "__" + $('.dr2c3').val()
            + "__" + $('.dr3c1').val() + "__" + $('.dr3c2').val() + "__" + $('.dr3c3').val();
    var h = '5';

    var it = 0;
    var cellB = '';
    var cellJ = '';
    var cellK = '';

    for (it = 1; it < 5; it++) {
        cellB = '.b' + it;
        cellJ = '.jc' + it;
        cellK = '.k' + it;


        var bb1 = $(cellB + 'r1c1').val();
        var bb2 = $(cellB + 'r1c2').val();

        var bb3 = $(cellB + 'r2c1').val();
        var bb4 = $(cellB + 'r2c2').val();

        var bb5 = $(cellB + 'r3c1').val();
        var bb6 = $(cellB + 'r3c2').val();

        var jj = $(cellJ).val();

        $.ajax({
            url: '/M08CDECUStructuralOptimiser/ServletMatrix1?action=calculateJ',
            data: {matrixB: "" + bb1 + "__" + bb2 + "__" + bb3 + "__" + bb4 + "__" + bb5 + "__" + bb6, matrixD: matD, valJ: jj},
            dataType: "json",
            success: function(response) {
                $(cellK+'1').val(response[0]);
                $(cellK+'2').val(response[1]);
                $(cellK+'3').val(response[2]);

                $(cellK+'4').val(response[3]);
                $(cellK+'5').val(response[4]);
                $(cellK+'6').val(response[5]);

                $(cellK+'7').val(response[6]);
                $(cellK+'8').val(response[7]);
                $(cellK+'9').val(response[8]);


            }
        });
    }
}

在小服务程序上

try {
            if (action.compareTo("calculateJ") == 0) {
                String matrixStringB = request.getParameter("matrixB");




                String[] tempStringArray;
                tempStringArray = matrixStringB.split("__");

                double[][] array = {{Double.parseDouble(tempStringArray[0]), Double.parseDouble(tempStringArray[1])},
                    {Double.parseDouble(tempStringArray[2]), Double.parseDouble(tempStringArray[3])},
                    {Double.parseDouble(tempStringArray[4]), Double.parseDouble(tempStringArray[5])}};


                Matrix b = new Matrix(array);
                Matrix bT = b.transpose();

                Matrix c = b.times(bT);

                //System.out.println(Arrays.deepToString(c.getArray()));

                String matrixStringD = request.getParameter("matrixD");

                String[] tempStringArray2;
                tempStringArray2 = matrixStringD.split("__");

                double[][] arrayD = {{Double.parseDouble(tempStringArray2[0]), Double.parseDouble(tempStringArray2[1]), Double.parseDouble(tempStringArray2[2])},
                    {Double.parseDouble(tempStringArray2[3]), Double.parseDouble(tempStringArray2[4]), Double.parseDouble(tempStringArray2[5])},
                    {Double.parseDouble(tempStringArray2[6]), Double.parseDouble(tempStringArray2[7]), Double.parseDouble(tempStringArray2[8])}};

                Matrix d = new Matrix(arrayD);


                Matrix r1 = c.times(d);

                String matrixStringJ = request.getParameter("valJ");
                double valj = Double.parseDouble(matrixStringJ);

                Matrix r2 = r1.times(valj);


                double valh = 1.5;

                Matrix r3 = r2.times(valh);


                double[][] resultArray = r3.getArray();
                double[] oneDArray = new double[resultArray.length * resultArray.length];
                //Flatten 2D array to 1D array...
                int s = 0;
                for (int i = 0; i < resultArray.length; i++) {
                    for (int j = 0; j < resultArray.length; j++) {
                        oneDArray[s] = resultArray[i][j];
                        s++;
                    }
                }

                Gson gson = new GsonBuilder().setPrettyPrinting().create();
                String json = gson.toJson(oneDArray);

                //System.out.println(oneDArray.length);
                System.out.println(Arrays.deepToString(r3.getArray()));
                out.println(json);
            }

        } finally {
        }

现在在控制台上我得到了所有 4 个数组并且有正确的值但是在站点上只有最后一个 k 值显示 k41,k42,.......k49

如何解决这个问题?

当我尝试将 js 循环限制为 <2 时,它会正确显示 k11、k12、......k19 值。

html:

<div class="showK" >
                    <table>
                        <tr>
                            <td>
                                K1
                            </td>
                            <td>
                                <input type="text" class="k11" value="" />
                                <input type="text" class="k12" value="" />
                                <input type="text" class="k13" value="" />

                                <br />

                                <input type="text" class="k14" value="" />
                                <input type="text" class="k15" value="" />
                                <input type="text" class="k16" value="" />
                                <br />
                                <input type="text" class="k17" value="" />
                                <input type="text" class="k18" value="" />
                                <input type="text" class="k19" value="" />
                                <br />

                            </td>
                        </tr>

                        <tr>
                            <td>
                                K2
                            </td>
                            <td>
                                <input type="text" class="k21" value="" />
                                <input type="text" class="k22" value="" />
                                <input type="text" class="k23" value="" />

                                <br />

                                <input type="text" class="k24" value="" />
                                <input type="text" class="k25" value="" />
                                <input type="text" class="k26" value="" />
                                <br />
                                <input type="text" class="k27" value="" />
                                <input type="text" class="k28" value="" />
                                <input type="text" class="k29" value="" />
                                <br />

                            </td>
                        </tr>

                        <tr>
                            <td>
                                K3
                            </td>
                            <td>
                                <input type="text" class="k31" value="" />
                                <input type="text" class="k32" value="" />
                                <input type="text" class="k33" value="" />

                                <br />

                                <input type="text" class="k34" value="" />
                                <input type="text" class="k35" value="" />
                                <input type="text" class="k36" value="" />
                                <br />
                                <input type="text" class="k37" value="" />
                                <input type="text" class="k38" value="" />
                                <input type="text" class="k39" value="" />
                                <br />

                            </td>
                        </tr>
                        <tr>
                            <td>
                                K4
                            </td>
                            <td>
                                <input type="text" class="k41" value="" />
                                <input type="text" class="k42" value="" />
                                <input type="text" class="k43" value="" />

                                <br />

                                <input type="text" class="k44" value="" />
                                <input type="text" class="k45" value="" />
                                <input type="text" class="k46" value="" />
                                <br />
                                <input type="text" class="k47" value="" />
                                <input type="text" class="k48" value="" />
                                <input type="text" class="k49" value="" />
                                <br />

                            </td>
                        </tr>
                    </table>
                </div>
4

1 回答 1

1

ajax 调用是异步的,所以当它接收到服务器的响应时,变量cellB,cellK的值cellK已经在循环中改变了。解决方案是在调用函数时将调用添加到函数中,如下例所示doAjaxCall

    function calculateK() {


        var matD = "" + $('.dr1c1').val() + "__" + $('.dr1c2').val() + "__" + $('.dr1c3').val()
                + "__" + $('.dr2c1').val() + "__" + $('.dr2c2').val() + "__" + $('.dr2c3').val()
                + "__" + $('.dr3c1').val() + "__" + $('.dr3c2').val() + "__" + $('.dr3c3').val();
        var h = '5';

        var it = 0;
        var cellB = '';
        var cellJ = '';
        var cellK = '';

        for (it = 1; it < 5; it++) {
            cellB = '.b' + it;
            cellJ = '.jc' + it;
            cellK = '.k' + it;
            doAjaxCall(cellB, cellJ, cellK);

        }
    }

function doAjaxCall(cellB, cellJ, cellK){
   var bb1 = $(cellB + 'r1c1').val();
            var bb2 = $(cellB + 'r1c2').val();

            var bb3 = $(cellB + 'r2c1').val();
            var bb4 = $(cellB + 'r2c2').val();

            var bb5 = $(cellB + 'r3c1').val();
            var bb6 = $(cellB + 'r3c2').val();

            var jj = $(cellJ).val();

            $.ajax({
                url: '/M08CDECUStructuralOptimiser/ServletMatrix1?action=calculateJ',
                data: {matrixB: "" + bb1 + "__" + bb2 + "__" + bb3 + "__" + bb4 + "__" + bb5 + "__" + bb6, matrixD: matD, valJ: jj},
                dataType: "json",
                success: function(response) {
                    $(cellK+'1').val(response[0]);
                    $(cellK+'2').val(response[1]);
                    $(cellK+'3').val(response[2]);

                    $(cellK+'4').val(response[3]);
                    $(cellK+'5').val(response[4]);
                    $(cellK+'6').val(response[5]);

                    $(cellK+'7').val(response[6]);
                    $(cellK+'8').val(response[7]);
                    $(cellK+'9').val(response[8]);


                }
            });
}
于 2013-06-25T20:41:01.147 回答