javascript - 带有 html 输入控件的动态生成的表行在回发时消失

标签 javascript jquery asp.net-mvc html razor

我使用 jquery 动态生成了一个带有 html 输入控件的表行。

enter image description here

问题是当有回发时它们就会消失。

如何让他们留下来?

HTML:

   @Html.HiddenFor(x => x.Attribute_Count)
                        <table id="attribute" class="list">
                            <thead>
                                <tr>
                                    <td class="left">Name</td>
                                    <td class="right">Sort Order</td>
                                    <td></td>
                                </tr>
                            </thead>

                            <tbody id="attribute-row">
                                <tr>
                                    <td class="left">

                                        @Html.TextBoxFor(x => x.AttributeName)
                                        <div class="validation-area">
                                            @Html.ValidationMessageFor(x => x.AttributeName)
                                        </div>
                                    </td>
                                    <td class="right">@Html.TextBoxFor(x => x.Attribute_SortOrder)</td>
                                    <td class="left"></td>
                                </tr>
                            </tbody>


                            <tfoot>
                                <tr>
                                    <td colspan="2"></td>
                                    <td class="left"><a onclick="addattribute();" class="button">+</a></td>
                                </tr>
                            </tfoot>
                        </table>

JQuery:

var attribute_row = 1; function addattribute(){html = '<tbody id="attribute-row' + attribute_row + '">';html += '  <tr>';html += '    <td class="left">@Html.TextBoxFor(x=>x.AttributeName)<div class="validation-area">@Html.ValidationMessageFor(x => x.AttributeName)</div></td>';html += '    <td class="right"><input type="text" name="Attribute_SortOrder" id="Attribute_SortOrder"></td>';html += '    <td class="left"><a onclick="$(\'#attribute-row' + attribute_row + '\').remove();" class="button">-</a></td>';html += '  </tr>';html += '</tbody>';$('#attribute tfoot').before(html);attribute_row++;$('#Attribute_Count').val(attribute_row);}

最佳答案

我解决了这个问题。

@for (var i = 0; i < Model.Attribute_Count; ++i)
                        {
                            <tbody id="attribute-row">
                                <tr>
                                    <td class="left">

                                        @Html.TextBoxFor(x => x.AttributeName[i])
                                        <div class="validation-area">
                                            @Html.ValidationMessageFor(x => x.AttributeName[i])
                                        </div>
                                    </td>
                                    <td class="right">@Html.TextBoxFor(x => x.Attribute_SortOrder[i])</td>
                                    <td class="left"><a onclick="$('#attribute-row' + i).remove();" class="button">-</a></td>
                                </tr>
                            </tbody>
}

谢谢

关于javascript - 带有 html 输入控件的动态生成的表行在回发时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23247806/

相关文章:

javascript - 基于for循环添加div类

javascript - 将一些数据提取到其中后,使文本框只读

javascript - 如何使用 JS 或 JQuery 对表格进行滚动加载?

javascript - KNockout JS - 自动重新加载 ajax 调用

jquery - 如何旋转 4 张(或更多)图像,并在每张图像之间淡入淡出?

ASP.NET(MVC)Outputcache和并发请求

javascript - 发出 GET 请求时 Node 路由不起作用

javascript - 无法在 Visual Studio 中注释 JavaScript

asp.net-mvc - 附加到 HtmlHelper 扩展方法中的routeValues

javascript - Viewbag 改变 onclick 中的值?