我目前在.net MVC4中执行项目,并且当编辑窗体中勾选复选框时,我有要求用当前日期填充显示字段。如何根据复选框动作自动填充日期Mvc4
由于它是一个编辑窗体复选框,可能已打勾,我需要检查它是否第一次选择,然后我需要填充当前日期。
更新:
马特Thanks..I使用隐藏域得到这个周围。 涉及的步骤: 1.将模型字段存储在编辑屏幕的隐藏字段中。 @ Html.Hidden( “hiddenDateField”,Model.DateField) @ Html.Hidden( “hiddenCheckBox”,Model.CheckBoxField)
- 然后使用JQuery我添加以下逻辑..
$ ('#chkBox')。on('click',function(){ var myDate = new Date(); var displayDate =(myDate.getUTCDate())+'/'+(myDate.getUTCMonth()+ 1 )+'/'+ myDate.getUTCFullYear()+“”+ myDate.getUTCHours()+“:”+ myDate.getUTCMinutes()+“:”+ myDate.getUTCSeconds();
if ($('#chkBox').is(":checked")) {
if ($('#hiddenCheckBox').val() == "False") { // This condition is to check if the user is selecting checkbox for 1st time by comparing with received model data value.
$('#lblDate').text(displayDate);
} else {
$('#lblDate').text($('#hiddenDateField').val()); // This condition is to restore the Date if user unticks and Select Checkbox again.
}
}
if (!$('#chkBox').is(":checked")) { // This when user unticks the checkbox and I replaced the date to empty char.
$('#lblDate').text("");
}
});
满足你的代码,我可以尝试使用jQuery和Ajax,但要知道,如果我们使用HTML帮助有备用选项。而且我是新的.net MVC .. – RajGan