在 JavaScript 中使用 JSON 值选中复选框 mvc.net



我把一个复选框绑定到这样的模型上。

@Html.CheckBoxFor(model => model.ReportSchedule.Monday, new {id="boolmonday" ,name = "boolmonday",@class="dayChkbox", disabled = "disabled" })<label>Monday</label><br />

现在我调用一个 JavaScript 方法:

$.get((URI.settings.getReportSchedule + '?scheduleId=' + id), function (data, status) {
    document.getElementById("boolmonday").checked = data.monday;
});

该函数成功完成并检索 JSON 对象。我已经使用警报进行了检查。状态为成功,并检索数据。在模型数据是这样的。

var data = new
{
    reportSchedulType =repeatschedule.ReportSchedulType,
    monday=true,
    tuesday = false,
    wednesday =true,
    thursday = false,
    friday=true,
};
return Json(data, JsonRequestBehavior.AllowGet);

但该复选框未更新。

试试这个:

$.get((URI.settings.getReportSchedule + '?scheduleId=' + id), function (data, status) {
    document.getElementById("boolmonday").checked = data.monday == 'True' ? true : false;
});

我已经解决了问题.我正在使用jquery 1.6+。

    $.get((URI.settings.getReportSchedule + '?scheduleId=' + id), function (data, success) {
        data.monday === true ? $("#ReportSchedule_Monday").prop('checked', true) : $("#ReportSchedule_Monday").prop('checked', false);
    });

使用低于 1.6 的 jQuery 的人应该使用

   $("#ReportSchedule_Monday").attr('checked', 'checked') 

而不是 .prop,因为它不适用于这些版本。

最新更新