How to Remove last Comma?

后端 未结 12 1916
自闭症患者
自闭症患者 2020-12-08 06:58

This code generates a comma separated string to provide a list of ids to the query string of another page, but there is an extra comma at the end of the string. How can I re

12条回答
  •  失恋的感觉
    2020-12-08 07:58

    You can use the String.prototype.slice method with a negative endSlice argument:

    n = n.slice(0, -1); // last char removed, "abc".slice(0, -1) == "ab"
    

    Or you can use the $.map method to build your comma separated string:

    var s = n.map(function(){
      return $(this).val();
    }).get().join();
    
    alert(s);
    

提交回复
热议问题