C# 如何基于当前月份使用Javascript将输入字段设置为只读
应用程序: ASP.NETMVC5应用程序 表格: 【Id、雇主、ITO编号、说明、技术编号、技术版本、级别、学分、期限、部门、状态、批准,1月、2月、3月、4月、5月、6月、7月、8月、9月、10月、11月、12月】 要求: 使用Javascript根据当前月份设置要编辑的月份字段 例如:当前月份为[Apr],则在编辑页面/表单上,只有表中的所有字段应设置为只读。除了[四月]C# 如何基于当前月份使用Javascript将输入字段设置为只读,c#,javascript,html,asp.net,asp.net-mvc,C#,Javascript,Html,Asp.net,Asp.net Mvc,应用程序: ASP.NETMVC5应用程序 表格: 【Id、雇主、ITO编号、说明、技术编号、技术版本、级别、学分、期限、部门、状态、批准,1月、2月、3月、4月、5月、6月、7月、8月、9月、10月、11月、12月】 要求: 使用Javascript根据当前月份设置要编辑的月份字段 例如:当前月份为[Apr],则在编辑页面/表单上,只有表中的所有字段应设置为只读。除了[四月] \u layout.cshtml <!DOCTYPE html> <html> <he
\u layout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@*@Scripts.Render("~/bundles/modernizr")*@
<script type="text/javascript" src="ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Pipeline Data", "Index", "Home", null, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
<li>@Html.ActionLink("Pipeline", "Index", "Pipeline")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
@model PipelineApp.Models.Pipelinedata
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Pipelinedata</h4>
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Employer, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Employer)*@
@Html.TextBoxFor(model => model.Employer, new { @readonly = "true" })
@Html.ValidationMessageFor(model => model.Employer)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ITONumber, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.ITONumber)*@
@Html.TextBoxFor(model => model.ITONumber, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.ITONumber)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Description)*@
@Html.TextBoxFor(model => model.Description, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.Description)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TECNumber, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.TECNumber)*@
@Html.TextBoxFor(model => model.TECNumber, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.TECNumber)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TECVersion, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.TECVersion)*@
@Html.TextBoxFor(model => model.TECVersion, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.TECVersion)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Level, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Level)*@
@Html.TextBoxFor(model => model.Level, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.Level)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Credits, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Credits)*@
@Html.TextBoxFor(model => model.Credits, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.Credits)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Duration, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Duration)*@
@Html.TextBoxFor(model => model.Duration, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.Duration)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Sector, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Sector)*@
@Html.TextBoxFor(model => model.Sector)
@Html.ValidationMessageFor(model => model.Sector)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Status, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Status)*@
@Html.TextBoxFor(model => model.Status, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.Status)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Approval, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Approval)*@
@Html.TextBoxFor(model => model.Approval, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.Approval)
</div>
</div>
<script type="text/javascript">
//alert("test")
$("document").ready(function () {
var today = new Date();
var month = today.getMonth() + 1;
$('.month').each(function () {
if ($(this).attr('id') == month) {
$(this).attr("disabled", false);
//$(this).removeAttr('readonly');
} else {
$(this).attr("disabled", true);
//$(this).attr('readonly', 'readonly');
}
});
});
</script>
<div id="" class="form-group">
@Html.LabelFor(model => model.Jan, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Jan)*@
@Html.TextBoxFor(model => model.Jan, new { id = "1", @class = "month" })
@Html.ValidationMessageFor(model => model.Jan)
</div>
</div>
<div id="" class="form-group">
@Html.LabelFor(model => model.Feb, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Feb)*@
@Html.TextBoxFor(model => model.Feb, new { id = "2", @class = "month" })
@Html.ValidationMessageFor(model => model.Feb)
</div>
</div>
<div id="" class="form-group">
@Html.LabelFor(model => model.Mar, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Mar)*@
@Html.TextBoxFor(model => model.Mar, new { id = "3", @class = "month" })
@Html.ValidationMessageFor(model => model.Mar)
</div>
</div>
<div id="" class="form-group">
@Html.LabelFor(model => model.Apr, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Apr)*@
@Html.TextBoxFor(model => model.Apr, new { id = "4", @class = "month" })
@Html.ValidationMessageFor(model => model.Apr)
</div>
</div>
<div id="" class="form-group">
@Html.LabelFor(model => model.May, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.May)*@
@Html.TextBoxFor(model => model.May, new { id = "5", @class = "month" })
@Html.ValidationMessageFor(model => model.May)
</div>
</div>
<div id="" class="form-group">
@Html.LabelFor(model => model.Jun, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Jun)*@
@Html.TextBoxFor(model => model.Jun, new { id = "6", @class = "month" })
@Html.ValidationMessageFor(model => model.Jun)
</div>
</div>
<div id="" class="form-group">
@Html.LabelFor(model => model.Jul, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Jul)*@
@Html.TextBoxFor(model => model.Jul, new { id = "7", @class = "month" })
@Html.ValidationMessageFor(model => model.Jul)
</div>
</div>
<div id="" class="form-group">
@Html.LabelFor(model => model.Aug, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Aug)*@
@Html.TextBoxFor(model => model.Aug, new { id = "8", @class = "month" })
@Html.ValidationMessageFor(model => model.Aug)
</div>
</div>
<div id="" class="form-group">
@Html.LabelFor(model => model.Sep, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Sep)*@
@Html.TextBoxFor(model => model.Sep, new { id = "9", @class = "month" })
@Html.ValidationMessageFor(model => model.Sep)
</div>
</div>
<div id="" class="form-group">
@Html.LabelFor(model => model.Oct, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Oct)*@
@Html.TextBoxFor(model => model.Oct, new { id = "10", @class = "month" })
@Html.ValidationMessageFor(model => model.Oct)
</div>
</div>
<div id="" class="form-group">
@Html.LabelFor(model => model.Nov, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Nov)*@
@Html.TextBoxFor(model => model.Nov, new { id = "11", @class = "month" })
@Html.ValidationMessageFor(model => model.Nov)
</div>
</div>
<div id="" class="form-group">
@Html.LabelFor(model => model.Dec, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Dec)*@
@Html.TextBoxFor(model => model.Dec, new { id = "12", @class = "month" })
@Html.ValidationMessageFor(model => model.Dec)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
由于我是新手,请看一些关于应该做什么的适当说明。一如既往,我们感谢您的宝贵时间这是小提琴
把身份证给你所有的月份
比如1月1日,2月2日。。。十二月十二日
样品
一月@Html.EditorFor(model=>model.Jan,null,new{id=“1”,@class=“month”})
十一月
@Html.EditorFor(model => model.Nov,null,new {id="11",@class="month"})
然后在javascript中添加以下内容。这是最简单的方法
$(document).ready(function() {
var today=new Date();
var month=today.getMonth()+1;
$('.month').each(function(){
if($(this).attr('id')==month)
{
$(this).attr("disabled",false);
}
else
{$(this).attr("disabled",true);
}
});
});
如果您使用的是jquery 1.5或1.5-那么使用attr
而不是prop
。但是如果您使用的是jquery 1.6+,那么答案应该适合您 试试这个:
视图:
显然,该函数在应用程序中使用时不起作用。能够提醒月份变量值,以便javascript工作。将Jquery CDN包含在head\u layout.cshtml中。在Inputsjavascript开始工作之前,将函数放置在Edit.cshtml中,但不在我的应用程序上。Javascript似乎无法执行。如果您能解释一下为什么使用“null”,我们将不胜感激。脚本>尝试了document.ready函数。显然没有错误。请注意,@Styles.Render(“~/Content/css”)@Scripts.Render(“~/bundles/modernizer”)已经存在了,因为它似乎不起作用。我在答案中尝试了CDNsee更新,如果您放置cdn,然后删除@section Scripts{@Scripts.Render(“~/bundles/jqueryval”)},文档节中不应该有双引号(“”)
$(document).ready(function() {
var today=new Date();
var month=today.getMonth()+1;
$('.month').each(function(){
if($(this).attr('id')==month)
{
$(this).attr("disabled",false);
}
else
{$(this).attr("disabled",true);
}
});
});
<div id="" class="form-group">
@Html.LabelFor(model => model.Jan, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.TextBoxFor(model => model.Jan)*@
@Html.TextBoxFor(model => model.Jan, new { id = "1", @class = "month", @onload = "checkDate()" })
@Html.ValidationMessageFor(model => model.Jan)
</div>
</div>
function checkDate(){
var today=new Date();
var month=today.getMonth()+1;
$('.month').each(function(){
if($(this).attr('id')==month)
{
$(this).prop("disabled",false);
}
else
{
$(this).prop("disabled",true);
}
}