【发布时间】:2018-11-14 01:28:24
【问题描述】:
这是我的代码,我正在使用一个表格,该表格给了我一个特定的数字,比如说 1000,在我的 if 中,我希望能够使用该存储的数字,以便当我单击按钮时我的变量('total')将是 1000,我不知道如何得到它,我已经尝试过 item.Salario,Model.Salario 并且它不起作用,我什至不知道它是否可能
@model IEnumerable<Planilla.Models.Planillas>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Planillas</h2>
<p>
@Html.ActionLink("Agregar Planilla", "Nuevo", null, new { @class = "btn btn-
success" })
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Empleado.Nombre)
</th>
<th>
@Html.DisplayNameFor(model => model.Salario)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Empleado.Nombre)
</td>
<td>
$@Html.DisplayFor(modelItem => item.Salario)
</td>
<td>
@Html.ActionLink("Edit", "Modificar", new { id = item.id }, new
{ @class = "btn btn-primary" }) |
<button onclick="myFunction()">Calcular </button>
<p id="demo"></p>
<script>
function myFunction() {
var Salario = item.Salario;
var total = Salario;
var mtotal;
if (total >0) {
mtotal = "su total es " + total;
} else {
mtotal = "Salario tiene que ser mayor que 0";
}
document.getElementById("demo").innerHTML = mtotal;
}
</script>
</td>
</tr>
}
</table>
基本上
$@Html.DisplayFor(modelItem => item.Salario)
给了我一个特定的数字,我想将该数字显示到 我的 if 在以下部分中
function myFunction() {
var Salario = item.Salario;
所以当我点击按钮时,我会得到 item.Salario 在桌子上给我的数字。
我该怎么做?
【问题讨论】:
-
您应该使用
@前缀,例如var Salario = @item.Salario;。如果要从元素中获取其值,请将 id 放入item.Salario并使用$('#targetId').val()。