4
I’m creating a list of Checkbox
according to the selection of a Select
on the same screen. No event change
of select
is called via ajax
javascript
, the method that returns a Json
with list(value, text) and going through this list I would dynamically create my Checkbox
.
Problem: The Checkbox
appear but are without Text on the screen. despite HTML
begotten to be right in my view.
Code:
Method that returns Json:
SelectList lista = new SelectList(string.Empty, "SearchParameterId", "Name");
if (specificationId > 0)
{
lista = new SelectList(db.SearchParameters
.Include(s => s.Specification)
.Where(sp => sp.Specification.SpecificationId == specificationId
&& sp.isActive == 1).AsEnumerable(), "SearchParameterId", "Name");
}
return Json(lista);
Javascript
of View
:
$("#SpecificationId").change(function () {
$("#ParamsCheckboxes").empty();
$.ajax({
type: 'POST',
url: '/DropdownCascade/GetSearchParametersBySpecification',
dataType: 'json',
data: { id: $("#SpecificationId").val() },
success: function (categories) {
$.each(categories, function (i, category) {
alert(category.Text);
alert(i);
var br = document.createElement('br');
var input = document.createElement('input');
input.type = 'checkbox';
input.name = category.Text;
input.id = category.Value;
input.value = category.Text;
input.textContent = category.Text;
document.getElementById("ParamsCheckboxes").appendChild(input);
document.getElementById("ParamsCheckboxes").appendChild(br);
});
},
error: function (ex) {
alert('Falha ao carregar as categorias N3.' + ex);
}
});
return false;
});
finally the dropdownlist and then the div I try to create the checkboxes inside:
<div class="form-group">
@Html.LabelFor(model => model.Specification,
htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.DropDownList("SpecificationId",
new SelectList(string.Empty, "Value", "Text"),
"Selecione a Sub-Categoria",
htmlAttributes: new { @id = "SpecificationId", @class = "form-control" })
</div>
</div>
<!-- START CHECKBOXES-->
<div class="form-group">
<div class="col-md-2">
Detalhes Técnicos:
<div id="ParamsCheckboxes">
</div>
</div>
</div>
<!-- END CHECKBOXES-->
It returns to the screen the data, I put one alert
before var checkBox = document.createElement('checkbox')
with Category. Text and it shows all values, it just does not create the Checkbox
.
Example of HTML
generated:
<div id="ParamsCheckboxes">
<input type="checkbox" name="1tb" id="2" value="1tb" style="color: black;"></input>
<br>
<input type="checkbox" name="5tb" id="3" value="5tb"></input>
<br>
<input type="checkbox" name="20tb" id="4" value="20tb"></input>
<br>
</div>
The style
was on my own, but that’s it. Any idea?
NOTE: It returns the completed list pro javascript, but does not create the elements
– rmelobarbosa