I am creating a list of Checkbox
according to the selection of a Select
on the same screen. In the event change
of select
is called via ajax
javascript
, the method that returns a Json
with list (value, text) and scrolling through this list I would dynamically create my% with%.
Problem: The Checkbox
appears, but there is no Text on the screen. although% generated% is right in my eyes.
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);
Checkbox
of HTML
:
$("#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 that I try to create the checkboxes in:
<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 the data to the screen, I put a Javascript
before View
with the category.Text and it shows all the values, it just does not create the alert
.
Example of% 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>
var checkBox = document.createElement('checkbox')
was on my own, but that's it. Any ideas ?