我知道禁用的输入不会将值发布到服务器。此外,复选框不能具有只读属性。我想获得“只读复选框”的功能,其中复选框被禁用,我可以读取页面帖子上复选框的值。
以下代码类似于我需要在我的应用程序中执行的操作。单击第一个复选框 (RememberMe) 时,我选中第二个复选框 (Seriously) 并为其添加禁用属性。
这是模型:
public class LogOnModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
[Display(Name = "Seriously?")]
public bool Seriously { get; set; }
}
这是我的看法:
@using (Html.BeginForm("", "", FormMethod.Post, new { id = "userForm" }))
{
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.Seriously)
@Html.LabelFor(m => m.Seriously)
</div>
<p>
<input type="button" value="Log On" onclick = "SubmitForm();"/>
</p>
</fieldset>
</div>
}
以下是视图中包含的我的 js 文件的精简内容:
function SubmitForm() {
$.ajax({
type: "POST",
url: "/Account/LogOnAjax",
cache: false,
data: $("#userForm").serialize(),
success: function (results) {
showMessage(results);
},
error:
showMessage("error!");
}
});
}
function SeriouslyCheckEnable(value) {
var SeriouslyCheckBox = $("input[name = 'Seriously']");
if (value == "true") {
SeriouslyCheckBox.attr('checked', 'checked');
SeriouslyCheckBox.attr("disabled", "true");
}
else {
SeriouslyCheckBox.removeAttr('checked');
SeriouslyCheckBox.removeAttr('disabled');
}
}
$(document).ready(function () {
$("input[name='RememberMe']").click(function (e) { SeriouslyCheckEnable(($("input[name='RememberMe']:checked").val())); });
});
这是我正在调试的控制器:
public ActionResult LogOnAjax(LogOnModel model)
{
bool seriously = model.Seriously;
bool remMe = model.RememberMe;
return Json("some message here", JsonRequestBehavior.AllowGet);
}
现在,无论认真复选框的选中状态如何,我总是认真地得到布尔变量的错误。任何帮助,将不胜感激。