简体   繁体   English

Javascript-输入不保留值?

[英]Javascript - Input not Keeping Values?

I have table in a razor-generated View . 我的桌子在剃刀产生的View A search box allows users to perform (you guessed it) a search operation by POSTing an AJAX request to a specified controller. 搜索框允许用户通过将AJAX请求发布到指定的控制器来执行(您猜对了)搜索操作。 The returned result is a PartialView (containing the new table + a pager) and I replace the existing table+pager with it. 返回的结果是PartialView(包含新表+分页器),我用它替换了现有的table + pager。

There is also a button to add rows (with two input fields + a save button) to the table. 还有一个按钮可以向表中添加行(具有两个输入字段和一个保存按钮)。 I want to "persist" those rows locally before the search is performed and then append them back to the table once I get the results from the AJAX request. 我想在执行搜索之前在本地“持久化”这些行,然后一旦从AJAX请求获得结果,便将它们附加回表中。

In my script you can see that I find all the "unsaved" rows (class .new-element-row ) and add them to an array before sending the search request. 在我的脚本中,您可以看到我找到了所有“未保存”的行(类.new-element-row )并将它们添加到数组中,然后发送搜索请求。 Then, after replacing the existing table with the new one, I append them again to the table: 然后,在用新表替换现有表之后,我将它们再次追加到表中:

newElements = new Array(); // unsaved rows

$(document).ready(function () {

        $('#searchbox').keyup(function () {

            var keyWords = $("#searchbox").val();

            ////////////////////////////////////////////////
            // Persist the non-saved rows///////////////////
            $("tr.new-element-row").each(function () {
                $this = $(this);
                var elem = $this[0];
                newElements.push(elem);
            });
            ////////////////////////////////////////////////

            // Send the search request
            $.ajax({
                url: '@(Url.Action("ActionName", "ControllerName"))',
                data: 'searchString=' + keyWords,
                cache: false,
                success: function (html) {

                    // Replace table with the one from the search results
                    $("#tableContainer").html(html);

                    ////////////////////////////////////////////////
                    // Re-append the unsaved rows to the new table//
                    newElements.forEach(function (entry) {
                        $("#table").append(entry.outerHTML);
                    });
                    ////////////////////////////////////////////////

                    // Clear...
                    newElements = new Array();
                    (...)
                }
            });

        });
});

It almost works well (ie rows are saved and appended again), if it wasn't for the fact that the input fields of the re-appended rows don't keep the text that I had inputted. 如果不是因为重新添加的行的输入字段不保留我输入的文本,它几乎可以很好地工作(即,保存并再次添加行)。

What am I doing wrong? 我究竟做错了什么? I'm probably missing something obvious, but I can't find out what it is. 我可能缺少明显的东西,但是我找不到它。

The problem is that you are pushing back not the element itself, but its HTML. 问题在于您不推回元素本身,而是推回HTML。 If you enter something into the text input, its HTML code does not change, although the DOM object sure does. 如果您在文本输入中输入内容,则其HTML代码不会更改,尽管DOM对象肯定会更改。

Not sure why you are using outerHTML here, but it seems that the simpliest fix is to remove it and just append the elements: 不知道为什么在这里使用outerHTML ,但是似乎最简单的解决方法是删除它,然后追加元素:

newElements.forEach(function (entry) {
    $("#table").append(entry);
});

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

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