繁体   English   中英

使用JavaScript Asp.Net MVC复制@ Html.DropdownList

[英]Clone @Html.DropdownList using javascript Asp.Net MVC

您好,我遇到以下问题:我有一个包含一些列的表,其中一个是已填充的@ Html.Dropdownlist。 我试图通过单击使用javascript的按钮来克隆它。 我可以克隆所有行,但是在克隆@ Html.Dropdownlist选择后,该行将停止工作。 我可以打开@ Html.Dropdownlist,但是无法选择选项。 谁能帮我? 提前致谢。

这是我的代码:

@{
 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错误,请尝试查看所有功能或脚本。 我会给您一个建议,删除所有内容,如果克隆的工作开始添加其余部分,则仅保留克隆的功能,并查看何时停止工作。 我认为这将是某些脚本或提供错误的类。 没关系

暂无
暂无

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

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