jQuery从具有相同名称的复选框数组中单击复选框

时间:2015-11-30 12:28:29

标签: jquery asp.net-mvc checkbox

我有一个Razor Partial View(ASP.NET MVC),我在其上有一组预验证,用于设置多个复选框的状态。

虽然正常运行正常,但我真的想要只捕获一个特定复选框(而不是整个复选框)的点击并执行'$ .post(...)'调用。

我将如何实现这一目标?这甚至可能吗?

这是(部分)我的部分视图:

    <div class="ukejuni">
    @{
        DateTime thisWeek = (DateTime)ViewData["thisWeek"];
        int thisWeekNumber = DateTimeExtender.WeekOfYearIso8601(thisWeek);
        int year = thisWeek.Year;
        string chkValue = thisWeekNumber + ";" + year;
    }
    Uke @Html.Raw(thisWeekNumber.ToString())
    @{
        switch ((DeliveryWeekType)Model.ba.delivery_week_type)
        {
            case DeliveryWeekType.EvenWeek:
                if (BusinessWeek.IsEvenWeek(thisWeekNumber)
                    && (Model.ba.deviations.Where(deviation =>
                        deviation.agreement_id == Model.ba.agreement_id
                        && deviation.week_nr == thisWeekNumber
                        && deviation.year == year
                        && deviation.delivery.HasValue
                        && (bool)deviation.delivery == true)
                    .FirstOrDefault() == null)
                )
                {
                    <input class="leveringicon" type="checkbox" name="chkDelivery" checked="checked" value="@chkValue" />
                }
                else
                {
                    <input class="leveringicon" type="checkbox" name="chkDelivery" value="@chkValue" />
                }
                break;
            case DeliveryWeekType.OddWeek:
                if (!BusinessWeek.IsEvenWeek(thisWeekNumber)
                       && (Model.ba.deviations.Where(deviation =>
                           deviation.agreement_id == Model.ba.agreement_id
                           && deviation.week_nr == thisWeekNumber
                           && deviation.year == year
                           && deviation.delivery.HasValue
                           && (bool)deviation.delivery == true)
                       .FirstOrDefault() == null)
                   )
                {
                    <input class="leveringicon" type="checkbox" name="chkDelivery" checked="checked" value="@chkValue" />
                }
                else
                {
                    <input class="leveringicon" type="checkbox" name="chkDelivery" value="@chkValue" />
                }
                break;
            default:
                if (Model.ba.deviations.Where(deviation =>
                    deviation.agreement_id == Model.ba.agreement_id
                    && deviation.week_nr == thisWeekNumber
                    && deviation.year == year
                    && deviation.delivery.HasValue
                    && (bool)deviation.delivery == true)
                    .FirstOrDefault() == null)
                {
                    <input class="leveringicon" type="checkbox" name="chkDelivery" checked="checked" value="@chkValue" />
                }
                else
                {
                    <input class="leveringicon" type="checkbox" name="chkDelivery" value="@chkValue" />
                }
                break;
        }
}
</div>

这是我用来捕获(并写入控制台)复选框值的脚本:

 $(document).ready(function () {
    $("input[name='chkDelivery']").change(function (e) {
        var values = $(this).val().split(';');
        console.log(values);

        if ($(this).is(':checked'))
        {
            @*$.post('@Html.Action("ChangeWeekDelivery")', {week: values[0], year: values[1], checkedState: true }, function () {

            });*@
            }
    });
});

在控制台输出中,我将获得8个具有相同输出的条目(8是屏幕上的复选框数量):

["51", "2015"]
["51", "2015"]
["51", "2015"]
["51", "2015"]
["51", "2015"]
["51", "2015"]
["51", "2015"]
["51", "2015"]

那么,如何将捕获事件限制为单个项目?我尝试过使用'$(e.target)'但结果仍然相同。

最好的问候

1 个答案:

答案 0 :(得分:0)

解决。

问题在于我将javascript代码放在Partial View中。 一旦我将JS放在主视图上,一切都很顺利。