首页 > 解决方案 > innerHTML 函数不会改变我的 td 的内容

问题描述

我有一个几乎可以工作的 ajax 函数,但当它到达 innerHTML 函数时却没有。当成功部分重新获得时,我正在尝试更改 td 内的文本。

在 MVC 项目的 .cshtml 文件中,我有这个简单的 HTML 和 JS :

// This td is not part of a loop, it is just inside a single table and tr

<td id="SommePour" style="min-width: 480px;" colspan="2">
        Somme pour @(Model.PbSelected?.Insert(4,"-").Insert(3,"-") ?? "000-0-00000")
</td>


<script>
    function NouveauTotal(noCompt9) {
        $.ajax({
            url: '@Url.Action("ChangeSousTotaux", "Explorer")',
            type: 'post',
            data: {
                postBudg9: noCompt9
            },
            success: function (result) {

                $("#SommePour").innerHTML = "<span>Somme pour " + '' + noCompt9 + ''.substring(0, 3) + "-" + '' + noCompt9 + ''.substring(3, 4) + "-" + '' + noCompt9 + ''.substring(4, 9) + "</span>";
            }
        });
    }
</script>

我什至尝试过:

$("#SommePour").innerHTML = 'yo';

我搜索过 :

使用 Javascript 更改 td 元素文本

.substring 错误:“不是函数”

但是我什么都没想到...

标签: javascripthtmlrazor

解决方案


如果你使用 jQuery,正确的语法是

$("#SommePour").html("What you want to add");

它应该是.html

来源:https ://www.w3schools.com/jquery/html_html.asp


推荐阅读