jquery - 通过jquery调用表时替换字符串不起作用
问题描述
我有一些名为get_periode
的页面包含类似这样的内容
<table>
<tr>
<td class="points">90000</td>
</tr>
</table>
此页面由 main_page 使用 ajax 调用。但是当我调用它时,替换字符串函数(每 3 个数字生成逗号)似乎不起作用
这是我的替换字符串函数
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}
$('.points').each(function(){
var v_pound = $(this).html();
v_pound = numberWithCommas(v_pound);
$(this).html(v_pound)
})
它仅在我打开页面get_periode
本身时才有效,但是当我通过 ajax 打开它时main_page
,该功能不起作用。我已经将该功能放在main_page
, 和get_periode
页面上
抱歉,我之前没有提到,但是当我更改主页中的选择时,我的 get_periode 页面已加载
解决方案
问题是页面是通过 AJAX 加载的。
您只需要在页面加载后调用您的函数。
这里有一个例子:
$("button").click(function(){
$.ajax({url: "mypage.html", success: function(result){
console.log("I have got a response!")
}});
});
console.log("Ciao!");
“再见!” 会显示在“I have got a response!”之前,因为对文件的调用是异步的
将您的函数放在<script>
加载页面内的标签中,或者仅在加载后调用您的函数(在响应函数中)