javascript - How to remove the comma at the end. . .
为情所困
为情所困 2017-06-14 10:52:00
0
3
774

$('.mask').on("click", '.skill-data-box_val p', function () {
    var res = $('.skillValue').text();
    var res1 = $(this).text()+",";
    if(res.indexOf(res1)>-1){
        $.alert("已选择");
    }else {
        res +=res1;
        var newStr = res.substring(0,res.length-1);
        console.log(newStr);
        $('.skillValue').text(res);
    }
});

If $('.skillValue').text(res)

The front end shows that the comma at the end is still there (this is for sure)

console.log(newStr)The comma at the end has been removed


But if $('.skillValue').text(newStr)

The front-end display is like this

The same goes for printing

I don’t quite understand. Please give me some advice. I will accept it with an open mind;

为情所困
为情所困

reply all(3)
phpcn_u1582

Create functions to process strings

function write(selector, res){
    let temp = res.slice(0, -1); 
    selector.text(temp);
}

That’s it:

Specific modifications

The code is modified to:

$('.mask').on("click", '.skill-data-box_val p', function () {
    var res = $('.skillValue').text();
    var res1 = $(this).text()+",";
    if(res.indexOf(res1)>-1){
        $.alert("已选择");
    }else {
        res +=res1;
        //res=res.substring(0,res.length-1);
        // 这里!! 
        write($('.skillValue'), res); 
    }
});
女神的闺蜜爱上我

Is this okay?:

var res = $('.skillValue').text(),
    dot = res == "" ? : ",",
    res1 = dot + $(this).text();

The main thing is to judge whether .skillValue is empty: if it is empty, it means that the added result is the first one, no comma is needed; if it is not empty, it means that there is already a selected value in it, then just add a comma in front of it. .

大家讲道理

A regular thing

"一行正则的事儿,".replace(/,$/,'')
//"一行正则的事儿"
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template