【发布时间】:2019-12-14 00:57:33
【问题描述】:
我正在创建一个 MVC 应用程序。我有一个模型,该模型具有布尔值和显示名称以及一个文本框。目的是每当在文本框中进行一些更改时,该名称应反映在复选框显示名称中。
型号:
[Required(ErrorMessage = "Please Enter Account Number")]
[Display(Name = "Account Number")]
public string AccountNumber{ get; set; }
[Display(Name = "You approving whatever you entered- Your enter AccountNumber is ")]
[Range(typeof(bool), "true", "true", ErrorMessage = "Please accept the Terms and Conditions by clicking the check the box!")]
public bool TermsAndConditions { get; set; }
查看:
<div class="form-group">
@Html.LabelFor(model => model.AccountNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AccountNumber, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@@Html.CheckBoxFor(x => x.TermsAndConditions)
@Html.LabelFor(model => model.TermsAndConditions)
@Html.ValidationMessageFor(x => x.TermsAndConditions)
</div>
JS:
$(function () {
$("#AccountNumber").keyup(function () {
var text= $("#AccountNumber").val();
alert('You type: '+text);
$("#TermsAndConditions").val(text);
});
});
当我运行页面时,无论我对帐号所做的更改如何,我都可以打印文本。但我无法将该值分配给复选框显示值。请帮帮我。
【问题讨论】:
-
您应该更新标签文本(显示属性名称值是放入标签的内容) - 而不是复选框,因此您需要为标签提供一个您可以定位的类/ID,或者我误解了你要做什么
标签: javascript jquery asp.net-mvc asp.net-mvc-4 model-view-controller