通过jQuery调用表时,替换字符串不起作用

时间:2018-08-16 12:36:26

标签: jquery html

我有一个名为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_pageget_periode页面上

抱歉,我之前没有提到,但是当我更改主页中的选择时,我的get_periode页面已加载

1 个答案:

答案 0 :(得分:0)

问题在于该页面是通过AJAX加载的。

仅在页面加载后才需要调用函数。

这里有个例子:

$("button").click(function(){
    $.ajax({url: "mypage.html", success: function(result){
        console.log("I have got a response!")
    }});
});

console.log("Ciao!");

“ C!”将在“我有回应!”之前显示,因为对该文件的调用是异步的

将函数放入已加载页面内的<script>标签中,或仅在加载后(在响应函数中)调用函数