[英]Copy textbox value to another - ASP.NET MVC Razor
I have the following form within an ASP.NET MVC 5 application 我在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")
}
How do I make it so that whenever a value is entered into the m.Email textbox that the value is immediately copied into the m.UserName textbox? 我如何做到这一点,以便无论何时在m.Email文本框中输入值,该值都会立即复制到m.UserName文本框中? (I will be making the UserName textbox un-editable after I have this working).
(执行此操作后,将使UserName文本框不可编辑)。
Thanks. 谢谢。
So if you want to just display the username, here is an example (vanilla js), something like: 因此,如果您只想显示用户名,这是一个示例(香草js),如下所示:
document.getElementById("txt2").onkeyup = function() { document.getElementById("txt1").value = this.value; };
<input type="text" id="txt1" disabled /> <input type="text" id="txt2" />
or here is the jquery version: 或这是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" />
I hope this helps. 我希望这有帮助。
Use javascript. 使用javascript。 Probably you have already included jQuery in your site scripts.
可能您已经在站点脚本中包含了jQuery。 So, all you have to do is to add following script:
因此,您要做的就是添加以下脚本:
$(function(){
$("#Email").change(function(){
$("#UserName").val($(this).val());
});
})
Please ensure about letter casing since I am not sure if Razor camelifies names or not - just preview HTML source in your browser and check the field names spelling. 请确保使用字母大小写,因为我不确定Razor是否将名称驼峰化-只需在浏览器中预览HTML源代码并检查字段名称的拼写即可。
Here is some working sample for you 这是给你的一些工作样品
Option 1 选项1
$("#Email").change(function(){
$("#UserName").val($(this).val());
});
Option 2 选项2
$("#Email").focusout(function(){
$("#UserName").val($(this).val());
});
and to enforce to use email as username, have this in your username textbox: 并强制使用电子邮件作为用户名,请在用户名文本框中输入以下内容:
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control", @disabled = "disabled" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.