2013-12-18 61 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

很高興你得到它的工作。如果我的回答有幫助,你會介意將其作爲答案嗎? –

相關問題