如何删除最后一个逗号? [英] How to Remove last Comma?
本文介绍了如何删除最后一个逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码生成逗号分隔的字符串,以便为另一页的查询字符串提供ID列表,但字符串末尾有一个额外的逗号。如何删除或避免使用额外的逗号?
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 remove or avoid that extra comma?
<script type="text/javascript">
$(document).ready(function() {
$('td.title_listing :checkbox').change(function() {
$('#cbSelectAll').attr('checked', false);
});
});
function CotactSelected() {
var n = $("td.title_listing input:checked");
alert(n.length);
var s = "";
n.each(function() {
s += $(this).val() + ",";
});
window.location = "/D_ContactSeller.aspx?property=" + s;
alert(s);
}
</script>
推荐答案
使用 Array.join
var s = "";
n.each(function() {
s += $(this).val() + ",";
});
变为:
var a = [];
n.each(function() {
a.push($(this).val());
});
var s = a.join(', ');
这篇关于如何删除最后一个逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文