我正在创建一个带有两个文本输入的视图('est'通过基于'idd'计算的'est'在上面计算出来,然后在 @foreach循环的表上创建一个表格,以显示全部我的数据。某些字段将基于彼此计算,但是每次" EST"和/或" IDD"输入更改时,必须在加载页面后计算其他字段。
。由于剃须刀是计算的服务器端,并且该JS客户端如何设置此类型的计算列,例如本示例中的一个:
This should be the myResult var (or 'est' value) + @Content.V2
i虽然要拉 @content.entityid设置一些动态var名称,例如:
<input type="text" id="@Content.EntityId-newfield"">
并在循环中拉一些<script>get the myResult again, add @Content.V2, set document.getElementById('@Content.EntityId-newfield').value</script>
,但行不通。
我该如何实现?
这是完整的页面:
<h1>My page</h1>
<br>
idd: <input type="text" id="idd" oninput="calculate()"> __ est: <input type="text" id="est">
<br><br>
<table border="1">
<thead>
<tr>
<th></th>
<th>Id</th>
<th>Title</th>
<th>V1</th>
<th>V2</th>
<th>V1 / V2</th>
<th>Field I know not how to calculate</th>
</tr>
</thead>
<tbody>
@foreach(var Content in AsDynamic(Data["Default"])){
<tr>
<td>@Edit.Toolbar(Content, actions: "edit,replace")</td>
<td>@Content.EntityId</td>
<td>@Content.Title</td>
<td>@Content.V1</td>
<td>@Content.V2</td>
<td>@Math.Round(@Content.V1 / @Content.V2, 2)</td>
<td>This should be the myResult var (or 'est' value) + @Content.V2</td>
</tr>
}
</tbody>
</table>
<script>
function calculate() {
var idade = document.getElementById('idd').value;
var myResult = (parseInt(idade) + 4) * 2;
document.getElementById('est').value = myResult;
}
</script>
想出一个功能数组将有能力。
@foreach之前:
var arrayOfFunctions = [];
<input type="text" id="peso" oninput="callFunctions()">
@foreach内部:
arrayOfFunctions.push(function(setPeso) { document.getElementById('@Content.EntityId').value = setPeso * @Content.fieldToCalculate; });
之后:@foreach:
function callFunctions() {
var myPeso = parseInt(document.getElementById('peso').value);
arrayOfFunctions.forEach(f => f(myPeso));
}