search

Home  >  Q&A  >  body text

javascript - How to remove the comma at the end. . .

$('.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;

为情所困为情所困2782 days ago807

reply all(3)I'll reply

  • phpcn_u1582

    phpcn_u15822017-06-14 10:54:00

    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); 
        }
    });

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-14 10:54:00

    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. .

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-14 10:54:00

    A regular thing

    "一行正则的事儿,".replace(/,$/,'')
    //"一行正则的事儿"

    reply
    0
  • Cancelreply