Добавить ввод и выбрать поле динамически с помощью jQuery ⇐ Jquery
-
Anonymous
Добавить ввод и выбрать поле динамически с помощью jQuery
In my below code I am trying to add rows dynamically on click on a button. It has been working smooth for the INPUT fields, but it fails to add row if we include SELECT field in the code.
Also post adding the row the data should get submitted for all tr added.
@if (Model != null && Model.Count > 0) { int j = 0; foreach (var i in Model) { @Html.TextBoxFor(a => a[j].Project,new { @required="required"}) @Html.TextBoxFor(a => a[j].Task, new { @required = "required" }) @Html.DropDownList("Status", ViewBag.EnumList as SelectList) @Html.TextBoxFor(a => a[j].UserId, new { @class = "id", @type = "hidden" }) @Html.TextBoxFor(a => a[j].Email,new { @class="email",@type="hidden"}) @Html.TextBoxFor(a => a[j].Date, new { @type="hidden"}) @if (j > 0) { //Remove Remove } j++; } jQuery as below
$("#addNew").click(function (e) { e.preventDefault(); var $tableBody = $("#dataTable"); var $trLast = $tableBody.find("tr:last"); var e = $(".email").val(); var id = $(".id").val(); var $trNew = $trLast.clone(); debugger; var suffix = $trNew.find(':input:first').attr('name').match(/\d+/); $trNew.find("td:last").html('Remove'); $.each($trNew.find(':input'), function (i, val) { // Replaced Name var oldN = $(this).attr('name'); var newN = oldN.replace('[' + suffix + ']', '[' + (parseInt(suffix) + 1) + ']'); $(this).attr('name', newN); //Replaced value var type = $(this).attr('type'); if (type.toLowerCase() == "text") { $("#id").attr('value', id); $("#email").attr('value', e); } // If you have another Type then replace with default value $(this).removeClass("input-validation-error"); }); $trLast.after($trNew); // Re-assign Validation var form = $("form") .removeData("validator") .removeData("unobtrusiveValidation"); $.validator.unobtrusive.parse(form); }); // 2. Remove $(document).on('click', '#delete', function () { $(this).parent().parent().remove(); });
Источник: https://stackoverflow.com/questions/778 ... ing-jquery
In my below code I am trying to add rows dynamically on click on a button. It has been working smooth for the INPUT fields, but it fails to add row if we include SELECT field in the code.
Also post adding the row the data should get submitted for all tr added.
@if (Model != null && Model.Count > 0) { int j = 0; foreach (var i in Model) { @Html.TextBoxFor(a => a[j].Project,new { @required="required"}) @Html.TextBoxFor(a => a[j].Task, new { @required = "required" }) @Html.DropDownList("Status", ViewBag.EnumList as SelectList) @Html.TextBoxFor(a => a[j].UserId, new { @class = "id", @type = "hidden" }) @Html.TextBoxFor(a => a[j].Email,new { @class="email",@type="hidden"}) @Html.TextBoxFor(a => a[j].Date, new { @type="hidden"}) @if (j > 0) { //Remove Remove } j++; } jQuery as below
$("#addNew").click(function (e) { e.preventDefault(); var $tableBody = $("#dataTable"); var $trLast = $tableBody.find("tr:last"); var e = $(".email").val(); var id = $(".id").val(); var $trNew = $trLast.clone(); debugger; var suffix = $trNew.find(':input:first').attr('name').match(/\d+/); $trNew.find("td:last").html('Remove'); $.each($trNew.find(':input'), function (i, val) { // Replaced Name var oldN = $(this).attr('name'); var newN = oldN.replace('[' + suffix + ']', '[' + (parseInt(suffix) + 1) + ']'); $(this).attr('name', newN); //Replaced value var type = $(this).attr('type'); if (type.toLowerCase() == "text") { $("#id").attr('value', id); $("#email").attr('value', e); } // If you have another Type then replace with default value $(this).removeClass("input-validation-error"); }); $trLast.after($trNew); // Re-assign Validation var form = $("form") .removeData("validator") .removeData("unobtrusiveValidation"); $.validator.unobtrusive.parse(form); }); // 2. Remove $(document).on('click', '#delete', function () { $(this).parent().parent().remove(); });
Источник: https://stackoverflow.com/questions/778 ... ing-jquery
Мобильная версия