dia*_*421 5 c# asp.net asp.net-mvc-4
我有 3 个文本框,第一个是数量,第二个是价格,第三个是总价。
<div class="form-group">
@Html.LabelFor(model => model.quantity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.quantity, new { htmlAttributes = new { @class = "form-control", @type = "number" } })
@Html.ValidationMessageFor(model => model.quantity, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.price, new { htmlAttributes = new { @class = "form-control", @type = "number" } })
@Html.ValidationMessageFor(model => model.price, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.totalprice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.totalprice, new { htmlAttributes = new { @class = "form-control", @type = "number" } })
@Html.ValidationMessageFor(model => model.totalprice, "", new { @class = "text-danger" })
</div>
Run Code Online (Sandbox Code Playgroud)
这是控制器:
[HttpPost]
public ActionResult Add(Model model)
{
obj.Quantity = model.quantity;
obj.Price = model.price;
obj.TotalPrice = model.totalprice
db.Details.Add(obj);
db.SaveChanges();
return RedirectToAction("");
}
Run Code Online (Sandbox Code Playgroud)
现在我想将第一个和第二个文本框的值相乘并在第三个文本框中显示它们。例如,如果用户在第一个文本框中输入 5,在第二个文本框中输入 100,那么它会自动在第三个文本框中显示 500,如果用户更改第一个或第二个文本框的值,则第三个文本框的值也应相应更改。谢谢。
您可以keyup在 javascript 中收听文本框的事件,读取值并进行乘法并将结果值设置为第三个文本框。
假设您的页面中包含 jQuery 库。
$(function(){
$("#quantity,#price").keyup(function(e){
var q=$("#quantity").val();
var p=$("#price").val();
var result="";
if(q!=="" && p!=="" && $.isNumeric(q) && $.isNumeric(p))
{
result = parseFloat(q)*parseFloat(p);
}
$("#totalPrice").val(result);
});
});
Run Code Online (Sandbox Code Playgroud)
这是一个有效的 jsbin 示例。
| 归档时间: |
|
| 查看次数: |
3970 次 |
| 最近记录: |