2sxc - 在 @foreach 循环中计算剃刀和 JS 之间的变量
2sxc - Calculated vars between razor and JS on a @foreach loop
我正在创建一个视图,顶部有两个文本输入('est' 通过 JS 根据 'idd' 计算),然后是一个带有 @foreach 循环的 table显示我所有的数据。一些字段将相互计算,但其他字段必须在页面加载后计算,每次 'est' and/or 'idd' 输入更改。
由于 razor 是计算服务器端和此 JS 客户端,我如何设置这些类型的计算列,如本例中的列:
This should be the myResult var (or 'est' value) + @Content.V2
我想拉动@Content.EntityId 来设置一些动态变量名称,例如:
<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));
}
我正在创建一个视图,顶部有两个文本输入('est' 通过 JS 根据 'idd' 计算),然后是一个带有 @foreach 循环的 table显示我所有的数据。一些字段将相互计算,但其他字段必须在页面加载后计算,每次 'est' and/or 'idd' 输入更改。
由于 razor 是计算服务器端和此 JS 客户端,我如何设置这些类型的计算列,如本例中的列:
This should be the myResult var (or 'est' value) + @Content.V2
我想拉动@Content.EntityId 来设置一些动态变量名称,例如:
<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));
}