gpt4 book ai didi

c# - 使用jquery迭代表并获取asp.net core中的值

转载 作者:行者123 更新时间:2023-12-01 08:34:44 25 4
gpt4 key购买 nike

我正在尝试迭代表以获取输入值。我怎样才能做到这一点?

尝试循环和迭代 tr 和类名称。 console.log 目前没有显示任何内容。

CSHTML

<tbody>
@foreach (var item in Model)
{
<tr>

<td>
@Html.DisplayFor(modelItem => item.PositionDebit)
</td>
<td class="Debit">
<span>@item.Debits</span>
<input class="Debits" type="number" value="@item.Debits" />
</td>
<td class="CoefPonderation">
<span>@item.CoefficientDePonderation</span>
<input class="CoefPonderation" type="number" value="@item.CoefficientDePonderation" />
</td>
<td class="Jauge">
<span>@item.Jauge</span>
<input class="Jauge" type="number" value="@item.Jauge" />
</td>

</tr>
}
</tbody>

Javascript表的id是tblDebit

  $("#tblDebit tr").each(function () {
var debit = {};
debit.Debits = $(this).find(".Debits").html();
debit.CoefPonderation = $(this).find(".CoefPonderation").html();
debit.Jauge = $(this).find(".Jauge").html();
console.log(debit.Debits);
console.log(debit.CoefPonderation);
alert(debit.CoefPonderation);
debits.push(debit);
});

最佳答案

您好,您遇到了一些问题:

  1. 首先使用 .val() 而不是 .html()。
  2. 您应该在 td 中使用 CoefPonderation 类,或者不要同时输入两者。
  3. 在循环中获取每个元素,但不要使用它。

这是 JavaScript 代码:

$("#tblDebit").find("tbody").children("tr").each(function(index, element) {
let debits = [],
debit = {};
debit.Debits = $(element).find(".Debits").val();
debit.CoefPonderation = $(element).find(".CoefPonderation").children('input[type="number"]').val();
debit.Jauge = $(element).find(".Jauge").val();
// console.log(debit.Debits);
// console.log(debit.CoefPonderation);
console.log('row:' + $.trim($(element).find("td").first().text()) + ', debit:' + debit.Debits + ', ponderation:' + debit.CoefPonderation);
debits.push(debit);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tblDebit" border="1">
<thead>
<th>row</th>
<th>Debits</th>
<th>Ponderation</th>
<th>Jauge</th>
</thead>
<tbody>
<tr>
<td>
1
</td>
<td class="Debit">
<span>item 1</span>
<input class="Debits" type="number" value="1" />
</td>
<td class="CoefPonderation">
<span>ponderation 1</span>
<input class="CoefPonderation" type="number" value="1" />
</td>
<td class="Jauge">
<span>Jauge 1</span>
<input class="Jauge" type="number" value="1" />
</td>
</tr>
<tr>

<td>
2
</td>
<td class="Debit">
<span>item 2</span>
<input class="Debits" type="number" value="2" />
</td>
<td class="CoefPonderation">
<span>ponderation 2</span>
<input class="CoefPonderation" type="number" value="2" />
</td>
<td class="Jauge">
<span>Jauge 2</span>
<input class="Jauge" type="number" value="2" />
</td>

</tr>
</tbody>
</table>

希望对你有帮助

关于c# - 使用jquery迭代表并获取asp.net core中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57978864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com