【问题标题】:innerHTML function not changing the content of my td [duplicate]innerHTML 函数不会改变我的 td 的内容 [重复]
【发布时间】:2019-08-27 20:21:33
【问题描述】:

我有一个几乎可以工作的 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';

我搜索过:

Changing td element text using Javascript

.substring error: "is not a function"

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

【问题讨论】:

  • 你正在混合一个 jQuery 对象和一个 DOM 属性。应该是$("#SommePour").html(...)
  • 一旦我们到达@Barmar,我是否可以使用 substring() 或者这将是相同的?
  • 当然可以。一个字符串不管你怎么得到它都是一样的。
  • 你为什么要在空字符串上调用.substring()
  • 我认为是 Razor 做的,而不是浏览器。

标签: javascript html razor


【解决方案1】:

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

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

应该是.html

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

【讨论】:

  • html 是一个函数,你必须调用它。
  • 是的,我的错,我会更正答案。
猜你喜欢
  • 1970-01-01
  • 2014-12-14
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
  • 2012-06-28
  • 1970-01-01
  • 2016-08-10
  • 1970-01-01
相关资源
最近更新 更多