我在 ASP.NET MVC 5 应用程序中有以下表单
@model ProjectOasis.Models.RegisterViewModel
@{
ViewBag.Title = "Register";
}
<h2>@ViewBag.Title.</h2>
@using (Html.BeginForm("Admin", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new account.</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label"})
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label"})
<div class="col-md-10">
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
</div>
</div>
//more form stuff
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Create" />
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
如何才能使每当在 m.Email 文本框中输入值时,该值立即复制到 m.UserName 文本框中? (完成此操作后,我将使用户名文本框不可编辑)。
谢谢。
最佳答案
因此,如果您只想显示用户名,这里有一个示例(vanilla js),如下所示:
document.getElementById("txt2").onkeyup = function() {
document.getElementById("txt1").value = this.value;
};
<input type="text" id="txt1" disabled />
<input type="text" id="txt2" />
或者这是 jquery 版本:
$("#txt2").on("keyup", function() {
$("#txt1").val($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txt1" disabled />
<input type="text" id="txt2" />
我希望这会有所帮助。
关于c# - 将文本框值复制到另一个 - ASP.NET MVC Razor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26082326/