I have a button that append's the same layout of html but I have a problem taking the select values with it. My html is:
<div id="degreePlusSign">Button</div>
<div class="padding">
<div class="col-md-5 col-xs-12">
<label for="prefix" class="sr-only">Degrees</label>
<select class="form-control marginBottom15">
@{
foreach (var degree in ViewBag.NewDegrees)
{
<option value="@degree.DegreeID" selected>@degree.DegreeName</option>
}
}
</select>
<span class="glyphicon form-control-feedback"></span>
</div>
</div>
JS:
$('#degreePlusSign').on('click', function () {
$(this).closest('.padding').append('<div class="padding mBottom"><i class="fa fa-times-circle fa-2x" aria-hidden="true"></i><div class="col-md-5 col-xs-12"><select class="form-control marginBottom15">@{foreach (var degree in ViewBag.NewDegrees){<option value="@degree.DegreeID" selected>@degree.DegreeName</option>}}</select><span class="glyphicon form-control-feedback"></span></div><div class="col-md-7 col-xs-12"><input class="form-control" placeholder="Major/Area of Study" type="text" /></div></div>');
});
Basically it recreates the html, but my problem is I'm using a foreach loop to bring in the values from the backend and it will only work with the inital container, not the duplicated containers afterwards. How do I keep the values on every duplication with the append jquery?
You have got two options:
{ var opts=new StringBuilder(); var sel="selected"; foreach(var d in ViewBag.NewDegrees) { opts.Append($"{d.DegreeName}"); sel=""; } }
Then somewhere down store it into a js variable:
var optsList="@(opts)";
Now you can use append new HTML as:
$('#degreePlusSign').on('click', function () {
$(this).closest('.padding').append('<div class="padding mBottom"><i class="fa fa-times-circle fa-2x" aria-hidden="true"></i><div class="col-md-5 col-xs-12"><select class="form-control marginBottom15">'+
optsList/*THIS IS THE VALUE WE STORED FROM C# CODE*/
+'</select><span class="glyphicon form-control-feedback"></span></div><div class="col-md-7 col-xs-12"><input class="form-control" placeholder="Major/Area of Study" type="text" /></div></div>');
});
select
element and use that: $('#degreePlusSign').on('click', function () { /*CLONE EXISTING SELECT ELEMENT. YOU MAY WANT TO PUT AN ID FOR SELECTION*/ var cl=$("select.form-control.marginBottom15").clone(); var d = $("").addClass("padding mBottom") append("").addClass("fa fa-times-circle fa-2x").attr("aria-hidden",'true'); /*APPEND CLONED SELECT TO INNER DIV*/ d.append("").addClass("col-md-5 col-xs-12").append(cl); d.append(cl); d.append("").addClass(glyphicon form-control-feedback"); d.append("").addClass("col-md-7 col-xs-12").append("").addClass("form-control") .attr("placeholder","Major/Area of Study").attr("type","text"); $(this).closest(".padding").append(d); });`
Hope you will be able fix any jQuery mess. I haven't used it since long.
.clone was what I was looking for. $('.padding').clone().append('.padding');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.