私は、mvcビューのFirstName、LastName、UserNameのフォームに3つのテキストボックスを持っています。 UserNameはFirstName + LastNameである必要があります。私はFirstNameフィールドの文字をFirstName.KeyUpイベントのUserNameに追加する次のJqueryを試しましたが、UserNameにLastNameフィールドの文字を追加すると、FirstName文字を上書きします(実際には、苗字)。これをJqueryと呼びます。 KeyUpはこれにはいいイベントですか?LostFocusなのでしょうか?または、returnキーワードで値を返す関数を使用する必要がありますか?そのアプローチのJqueryとは何ですか?同じMvcビュー内の他のテキストボックスからテキストボックスにテキストを追加するJquery
@using (Html.BeginForm()) {
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
</fieldset>
}
<script type="text/javascript">
$(function() {
$("#FirstName").keyup(function() {
$("#UserName").val(this.value);
});
$("#LastName").keyup(function() {
$("#UserName").val(this.value);
});
});
</script>