使用JQuery将值分配给HTML表格单元格

时间:2015-09-15 01:22:29

标签: javascript jquery html asp.net-mvc

有一个表格显示模型条目,每个字段指定一个唯一的div id,它组合了一个关键字和每一行的ID。当用户在表格的输入栏中输入数字时,脚本应该:获取单元格在同一行的位置;并基于其他单元的值改变两个预定单元的值。 似乎测试在最终更新之前是成功的。我尝试使用.val(),。value和.html(),结果单元格变为空白,如果脚本没有错误则显示0。有人请发布正确的jQuery命令及其工作原理吗?非常感谢提前。

表格:

<table id="dt_Positions" class="table table-striped">
    <thead>
        <tr>
            <th class="text-center">Month</th>
            <th class="text-center">Owed</th>
            <th class="text-center">Bought</th>
            <th class="text-center">Total Position</th>
            <th class="text-center">Non-Fixed</th>
            <th class="text-center">Fixed</th>
            <th class="text-center">Fixed Position</th>
            <th class="text-center">Proposed</th>
        </tr>
    </thead>
    <tbody>
        @if (Model.Forecasts.Any())
        {
            foreach (var record in Model.Summaries)
            {
                <tr>
                    <td id="nmonth@(record.fID)" align="center">@String.Format("{0:d}", @record.Month)</td>
                    <td id="ntotal@(record.fID)" align="center">@record.NTotal</td>
                    <td id="nbought@(record.fID)" align="center">@record.NBought</td>
                    <td id="ntposition@(record.fID)" align="center">@record.NTotalPosition</td>
                    <td id="nvariable@(record.fID)" align="center">@record.NVariable</td>
                    <td id="nfixed@(record.fID)" align="center">@record.NFixed</td>
                    <td id="nfposition@(record.fID)" align="center">@record.NFPosition</td>
                    <td id="ninput@(record.fID)" align="center"><input class="nInput" type="number" name="quantity" min="1" max="50000"></td>
                </tr>
            }
        }
    </tbody>
</table>

剧本:

@section Scripts
{
    <script src="~/Scripts/jquery-2.1.3.js"></script>

    <script type="text/javascript" language="javascript">
        $(function () {
            $('[id^=ninput]').keyup(function (e) {
                var $id = $(this).attr('id');
                var $i = $(this);
                var $idNum = $id.slice(6);
                var $tp = $('#ntposition' + $idNum);
                var $fp = $('#nfposition' + $idNum);
                var $nt = $('#ntotal' + $idNum);
                var $nh = $('#nbought' + $idNum);
                var $f = $('#nfixed' + $idNum);
                //The lines below appear to be the hiccup
                $tp.val($nh.val() + $i.html() - $nt.val());
                $fp.val($nh.val() + $i.html() - $f.val());
                debugger;
            });
        });
    </script>
}

编辑:返回&#34; NaN&#34;是: ntotal = 29,nbought = 5,ntposition = -24,nvariable = 3,nfixed = 26,nfposition = -21,所有看起来都是来自测试视图的int,但是ntotal,nbought和nfixed显示&#34; NaN& #34;在console.log中导致&#34; NaN&#34;在ninput = 5之后出现在测试视图中。

2 个答案:

答案 0 :(得分:2)

$i是文本框,因此要获得其值,您需要使用$i.val()。其他元素是表格单元格,因此要获取或设置所需的值.text(),而不是.val()。但是,您使用id属性使代码复杂化。相反,删除然后使用相对选择器

$('input').keyup(function() { // or $('.nInput').keyup
  var i = Number$(this).val());
  var cells = $(this).closest('tr').children('td');

  var tp = cells.eq(3);
  var fp = cells.eq(6);
  // Get current cell values as a number
  var nt = Number(cells.eq(1).text());
  var nh = Number(cells.eq(2).text());
  var f = Number(cells.eq(5).text());
  // Update totals
  tp.text(nh + i - nt);
  fp.text(nh + i - f);

});

附注:var i = $(this).val();的值可能是null但不确定您要如何处理此问题 - 可能只是使用

var i = $(this).val();
if (!i) {
  return; // don't do any calculations
}

答案 1 :(得分:1)

您需要了解val()text()html()

之间的区别
  • val()用于获取和设置表单元素的值,inputselect等。
  • text()用于获取和设置非表单元素的简单无格式文本。
  • html()用于从节点获取和设置内部Html

所以你想要的是:

$tp.text($nh.text() + $i.val() - $nt.text());
$fp.text($nh.text() + $i.val() - $f.text());

另外要小心,因为+是javascript中的数学加法字符串连接,因此您可能希望将字符串转换为适当的数字类型。