2013-12-18 57 views
-1

我目前在.net MVC4中执行项目,并且当编辑窗体中勾选复选框时,我有要求用当前日期填充显示字段。如何根据复选框动作自动填充日期Mvc4

由于它是一个编辑窗体复选框,可能已打勾,我需要检查它是否第一次选择,然后我需要填充当前日期。


更新:

马特Thanks..I使用隐藏域得到这个周围。 涉及的步骤: 1.将模型字段存储在编辑屏幕的隐藏字段中。 @ Html.Hidden( “hiddenDateField”,Model.DateField) @ Html.Hidden( “hiddenCheckBox”,Model.CheckBoxField)

  1. 然后使用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(""); 

    } 
}); 
+0

满足你的代码,我可以尝试使用jQuery和Ajax,但要知道,如果我们使用HTML帮助有备用选项。而且我是新的.net MVC .. – RajGan

回答

0

不需要使用ajax,只需使用jquery。没有你的代码,这是很难给出一个例子,所以你需要改变这

$(document).ready(function(){ 
    //this will check if checked on load and set the text box to now 
    if($('.chkClass').is(":checked")){ 
     $('.txtClass').val($.now()); 
    } 
    $('.chkClass').on('click', function(){ 
     //check again so it only sets the date if they check and not uncheck 
     if($('.chkClass').is(":checked")){ 
      $('.txtClass').val($.now()); 
     } 
    }); 
}); 
+0

马特谢谢..我通过使用隐藏字段得到了这个。步骤involed – RajGan

+0

很高兴你得到它的工作。如果我的回答有帮助,你会介意将其作为答案吗? –

相关问题