JavaScript 无法在 JSP 中正确生成 HTML

标签 javascript jquery jsp

我正在使用 javascript 在 jsp 页面中动态生成一些选择。该代码运行良好。但我遇到了一个问题,不知道如何处理。嗯,这是 JavaScript 代码:

 $('#addRow').click(function(){

        i++;

        //stuff categories
        var categoriesStr = '${stuffCategoriesListStr}';
        categoriesStr = categoriesStr.replace('[', '');
        categoriesStr = categoriesStr.replace(']', '');
        var categoriesList = categoriesStr.split(', ');

        var selectCategoriesHtml = "<select id='category"+i+"' style='width: 200px;' onchange=\"retrieveUrlAjax('ajaxstuff.action?category='+this.value, 'menusCombobox"+i+"')\"><option value='-1'>-- Select category --</option>";

        for (j = 0; j < categoriesList.length; j++) {

            var option = "<option value='"+categoriesList[j]+"'>"+categoriesList[j]+"</option>";
            selectCategoriesHtml += option;
        }

        selectCategoriesHtml += "</select>";
        //end os stuff categories

        //units list
        var unitsStr = '${unitsListStr}';
        unitsStr = unitsStr.replace('[','');
        unitsStr = unitsStr.replace(']','');
        var unitsList = unitsStr.split(', ');            

        var selectUnitsHtml = "<select id='unit+"+i+"' style='width: 200px;'><option value='-1'>-- Select unit --</option>";

        for (j = 0; j < unitsList.length; j++) {

            var option = "<option value='"+unitsList[j]+"'>"+unitsList[j]+"</option>";
            selectUnitsHtml += option;
        }

        selectUnitsHtml += "</select>";
        //end of units list

        var d = document.getElementById("fields");
        d.innerHTML += "<table width='100px'>\n\
                            <tr>\n\
                                <td>"+selectCategoriesHtml+"</td>\n\
                                <td width='100px;'><div class='menusCombobox"+i+"'><select style='width: 200px;'/></div></td>\n\
                                <td width='1%'><input type='text' style='width: 40px;' id='"+i+"' value='1'/></td>\n\
                                <td width='200px'>"+selectUnitsHtml+"</td>\n\
                            </tr>\n\
                        </table>";
    });        

这是 div:

 <div id="fields"></div>

结果:

1 个屏幕:http://img199.imageshack.us/img199/4662/screenshot1hrpl.png

2屏:http://img827.imageshack.us/img827/5717/screenshot2hn.png

问题是,当我在第一行中选择某些内容,然后添加另一行时,所选第一行中的所有数据都会被重置。您可以在第一个和第二个屏幕中看到这一点。我认为问题出在innerHTML中,但我还没有找到其他替代方案。也许你们可以帮助我。

最佳答案

我认为是 innerHTML 导致了您的问题,看看这个问题 -

Is it possible to append to innerHTML without destroying descendants' event listeners?

当您使用 jQuery 时,您可以使用类似 append 的东西吗?或after相反?

关于JavaScript 无法在 JSP 中正确生成 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7321585/

相关文章:

javascript - AngularJS:如何更改使用 ng-repeat 渲染的按钮文本 onclick

javascript - 编写一个我可以重新使用来表示 CurrentUser 的类的最佳方法

javascript - 标签中的 `language=JavaScript1.1` 有多旧?

javascript - ajax 成功确认表单后关闭 fancybox

javascript - 如何根据先前的子宽度计算 div 的宽度

java - Struts 2 - 捕获意外异常设置 'xx'

javascript - ANGULAR - 如果表单值未更改,则禁用提交按钮

jquery - 是否有任何 css 属性可以阻止任何动画隐藏元素的尝试?

java - servlet 过滤器如何识别下一个目标是另一个过滤器还是 servlet/jsp?

java - Spring MVC 将多个列表从 Controller 传递到 jsp