2015-04-23 52 views
1

我有一些代碼來設置隱藏字段的值,所以我可以在後面的代碼中訪問它,但在後面的代碼中該值始終爲空。 effectiveDate的值正在設置,但我看起來沒有設置隱藏字段屬性Value使用jquery設置隱藏字段的問題使用jquery

<input id="appEffectiveDate" type="text" /> 
<label id="effectiveDateLabel" for="appEffectiveDate">App Effective Date</label> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<asp:HiddenField ID="appEffectiveDateToSetForUserRole" runat="server" Value="" Visible="false" /> 
<script> 
$(function() { 
    var SelectedDates = {}; 
    $('#appEffectiveDate').datepicker({ 
     beforeShowDay: function (date) { 
      var Highlight = SelectedDates[date]; 
      if (Highlight) { 
       return [true, "Highlighted", Highlight]; 
      } 
      else { 
       return [true, '', '']; 
      } 
     } 
    }); 
    $("#effectiveDateLabel").hide(); 
    $("#appEffectiveDate").hide(); 
    $('input[value="85"]').click(function() { 
     if($(this).is(':checked')) 
     { 
      $("#effectiveDateLabel").show(); 
      $("#appEffectiveDate").show(); 
     } 
    }); 
    $("#appEffectiveDate").change(function() { 
     var effectiveDate = $("#appEffectiveDate").val(); 
     $(":asp(appEffectiveDateToSetForUserRole)").prop('value', effectiveDate); 
    }); 
}); 
</script> 

在值後面的代碼隱藏字段爲空:

if (!string.IsNullOrEmpty(appEffectiveDateToSetForUserRole.Value)) 
{ 
    // this is never called because .Value is empty 
} 

回答

3

如果Visible設置爲false,控制不會被ASP.NET中的標記在所有渲染,這意味着jQuery將無法找到它,因爲它不存在。只要刪除visible = false部分即可。它會保持隱藏。