如何在脚本中拆分字符串

How to split string in script

本文关键字:拆分 字符串 脚本      更新时间:2023-12-03

在此函数中

$(document).on("click",".change",function(){
    var id = this.id;
    $.ajax({
        url: "<?php echo base_url('discussions/reader/change_flag')?>",
        type: "post",
        //dataType:"Json",
        data: {
            "id": id
        },
        success: function (result) {
            alert(result);
        }
    });
});

id是"r_1",我想将其拆分为"r"answers"1",并分别使用它们。我以前用过爆炸,但没用。我能做什么?

您可以这样使用:

var id = this.id;//"r_1"
var rr = id.split('_');
//and use like this:
rr[0];//"r"
rr[1];//"1"

explode是一个PHP函数,因此它不能在javascript中工作。

使用split('_')