克隆@Html.使用javascript下拉列表.净MVC

Clone @Html.DropdownList using javascript Asp.Net MVC

本文关键字:MVC 下拉列表 javascript @Html 使用 克隆      更新时间:2023-09-26

你好,我有以下问题:我有一个有一些列的表,其中一个是@Html。已填充的下拉列表。我试图通过点击使用javascript的按钮克隆。我可以克隆所有行,但在我克隆@Html之后。下拉列表选择停止工作。我可以打开@Html。下拉列表,但我无法选择选项。有人能帮我吗?提前谢谢。

下面是我的代码:
@{
 DatabaseContext db = new DatabaseContext();
 var actions_list = new SelectList(db.Actions.Select(f => new { f.Id, f.Action }), "Id", "Action");
    }

        <table>
         <thead>
 <tbody id="lines-event">
          <tr>
           <th>#</th>
           <th>Action</th>
           <th>Data</th>
           <th><button type="button" class="btn glyphicon glyphicon-plus pull-right" id="add_row"></button></th>
          </tr>
        </thead>
        <tr id='line-new-event'>
         <td class="col-md-1"></td>
         <td class="action col-md-3">@Html.DropDownList("Actions", actions_list, htmlAttributes: new { @class = "form-control", data_width = "100%"})</td>
         <td class="data"><input type="text" class="form-control edit_data" /></td>
         <td class="col-sm-1"><button type='button' class='btn glyphicon glyphicon-trash pull-right delete_row'></button></td>
        </tr>
    </tbody>
    </table>

<script>
 $("#add_row").click(function () {
           function createGuid() {
                function s4() {
                    return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
                }
                return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
            }
            var guid = createGuid();
            console.log(guid);
            var newLine = $('#line-new-event').clone().removeAttr('id');
            $(newLine).find('select').attr("id", guid);
            $(newLine).find('select').attr("name", guid);
            $(newLine).insertBefore($('#line-new-event'))
        });
</script>

嗨,你的代码看起来不错,你确定不只是javascript错误,试着看看你所有的函数或脚本。我会给你一个建议,删除一切,只留下克隆的功能,如果它的工作开始添加其余的,看看它什么时候停止工作。我认为会有一些脚本或类给出错误。你欢迎