How to Remove last Comma?

后端 未结 12 1940
自闭症患者
自闭症患者 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:49

    Sam's answer is the best so far, but I think map would be a better choice than each in this case. You're transforming a list of elements into a list of their values, and that's exactly the sort of thing map is designed for.

    var list = $("td.title_listing input:checked")
        .map(function() { return $(this).val(); })
        .get().join(', ');
    

    Edit: Whoops, I missed that CMS beat me to the use of map, he just hid it under a slice suggestion that I skipped over.

提交回复
热议问题