[英]EditorFor default value from two other field (ASP.NET MVC)
I have problem to solve like in this image (click to show image) below. 我有问题要解决,如下图所示(单击以显示图像) 。 My view:
我的观点:
@model CodeSimplified.Models.User
@{
ViewBag.Title = "User";
}
<h2>User</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Value1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Value1)
@Html.ValidationMessageFor(model => model.Value1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Value2)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Value2)
@Html.ValidationMessageFor(model => model.Value2)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MultiplyValue1Value2)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MultiplyValue1Value2)
@Html.ValidationMessageFor(model => model.MultiplyValue1Value2)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back", "Index")
</div>
Result: 结果:
When I enter the value1 and value2, I would like to value in MultiplyValue1Value2 completes automatically as a result of the multiplication Value1 and Value2 当我输入value1和value2时,我想将MultiplyValue1Value2中的值作为Value1和Value2相乘的结果自动完成
How can I do this? 我怎样才能做到这一点?
You have to use javascprit to do that, with jQuery you can do this like that 您必须使用javascprit来做到这一点,使用jQuery可以像这样
$("#Value1, #Value2").on("input",function(){
var val1 = parseInt($('#Value1').val());
var val2 = parseInt($('#Value2').val());
if(!isNaN(val1) && !isNaN(val2))
$('#MultiplyValue1Value2').val(val1 * val2);
});
$("#Value1, #Value2").on("input",function(){ var val1 = parseInt($('#Value1').val()); var val2 = parseInt($('#Value2').val()); if(!isNaN(val1) && !isNaN(val2)) $('#MultiplyValue1Value2').val(val1 * val2); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="Value1" type="text"/> <input id="Value2" type="text"/> <input id="MultiplyValue1Value2" type="text"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.