c# - 将文本框值复制到另一个 - ASP.NET MVC Razor

标签 c# asp.net asp.net-mvc razor

我在 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/

相关文章:

c# - Emgu Opencv C# ConvertTo 不同的结果

C# 从 SQL DataTable 填充组合框

c# - Entity Framework MappingException : The type 'XXX has been mapped more than once

javascript - 我的下拉列表选择 onchange 没有在 Angular js 中触发

c# - Asp.NET 实时游戏

javascript - 使用 MVC、JavaScript 显示弹出窗口

c# - 为什么 Thread.Start 方法在 CPU 负载高时被阻塞?

c# - ASP .Net Core 2 自包含部署在不同应用程序池标识帐户下的 IIS 中出现 502.5 错误

c# - 自定义错误消息未在 ASP.NET MVC 4 中翻译

.net - 在 ASP.NET MVC 中添加对 View 的引用