【发布时间】: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