Something strange happens when set the checked attribute of a checkbox group to a MVC model.
When I set the checked prop in the view:
@foreach (FoodVM.filter item in Model.SelectedFood)
{
<input type="checkbox"
name="food"
class="filter-checkbox"
value="@item.name"
@(Model.SelectedFood.Contains(item) ? "checked" : "") />
}
...then the each loop does not accurately detect what is checked or unchecked. The state of the checked attrs never seems to change. When I uncheck a checkbox, it still falls into the :checked condition.
$('.checkbox-filter').click(function (e) {
var obj = {};
$("input.checkbox-filter:checked").each(function (index, el) {
var name = $(el).attr("name");
var val = $(el).val();
obj[name] = val;
//^ this always returns the original checked value
});
var url = '/c?' + $.param(obj);
});
Does it have something to do with the model in the page? In this instance, I do not want to use the Html helpers, I'd like to stick with the plain html input.
In HTML, everything works as expected: http://ift.tt/1Auw8RY
I even tried setting the prop('checked', true) through JQ to completely circumvent the setting of the checked prop through MVC, but that had no effect.
Aucun commentaire:
Enregistrer un commentaire