繁体   English   中英

2sxc-@foreach循环上的剃刀和JS之间的计算的var

[英]2sxc - Calculated vars between razor and JS on a @foreach loop

我正在创建一个视图,顶部有两个文本输入(“ est”是通过JS基于“ idd”通过JS计算的),然后是一个带有@foreach循环的表以显示我的所有数据。 每次“ est”和/或“ idd”输入更改时,某些字段将相互计算,而其他字段必须在页面加载后计算。

由于razor是在服务器端和此JS客户端计算的,因此如何设置这些计算列的类型,如本示例中所示:

This should be the myResult var (or 'est' value) + @Content.V2

我虽然要拉@ 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>在循环内<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));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM