美好的一天, 我有这个代码的视图
<ul id="check-list-box" class="list-group checked-list-box">
@for (var i = 0; i < Model.RespuestasParaDetalleRespuesta.Count(); i++)
{
<li class="list-group-item" data-color="danger">
@Html.HiddenFor(model => model.Something[i].Id)
@Html.HiddenFor(model => model.Something[i].DetailId)
@Html.CheckBoxFor(model => model.Something[i].IsSelected,new { @class="test"})@Model.Something[i].SomeText
</li>
}
</ul>
我的脚本也改变了每个复选框属性&#34;已检查&#34;
$(function () {
$('.list-group.checked-list-box .list-group-item').each(function () {
var $widget = $(this),
$checkbox = $('.test'), //I use this selector but is not a good one
....
more settings
};
$widget.css('cursor', 'pointer')
// Event Handlers
$widget.on('click', function () {
$checkbox.prop('checked', !$checkbox.is(':checked')); //changes propertie to checked
...more code
...more code
});
如您所见,使用此代码,一旦用户点击某个项目,该函数会尝试将复选框属性更改为选中或取消选中。 但是我意识到这种方法不起作用,因为当我点击一个项目时,会检查所有复选框。
每个复选框的ID如下所示
Something_0__IsSelected , Something_1__IsSelected
我该怎么做才能使$ checkbox选择器更具体?感谢
答案 0 :(得分:1)
您的$checkbox = $('.test')
正在选中所有复选框。您只需在相应的<li>
元素
$('.list-group.checked-list-box .list-group-item').each(function () {
// $(this) is the <li> element
var $widget = $(this),
$checkbox = $(this).find('.test'), // change this
....