html页面以逗号分割字符串,使用Javascript 从HTML列表框(多选)中的项生成逗号分隔的字符串...

因此,如果您有一个html列表框,也称为多选,并且您希望生成一个逗号分隔的字符串,列出该列表框中的所有值,您可以使用以下示例执行此操作. list_to_string()js函数是这里唯一重要的事情.你可以在http://josh.gourneau.com/sandbox/js/list_to_string.html玩这个页面

function list_to_string()

{

var list = document.getElementById('list');

var chkBox = document.getElementById('chk');

var str = document.getElementById('str');

textstring = "";

for(var i = 0; i < list.options.length; ++i){

comma = ",";

if (i == (list.options.length)-1){

comma = "";

}

textstring = textstring + list[i].value + comma;

str.value = textstring;

}

}

India

US

Germany

解决方法:

IE上的字符串连接非常慢,请使用数组:

function listBoxToString(listBox,all) {

if (typeof listBox === "string") {

listBox = document.getElementById(listBox);

}

if (!(listBox || listBox.options)) {

throw Error("No options");

}

var options=[],opt;

for (var i=0, l=listBox.options.length; i < l; ++i) {

opt = listBox.options[i];

if (all || opt.selected ) {

options.push(opt.value);

}

}

return options.join(",");

}

标签:javascript,textbox,listbox,string,html

来源: https://codeday.me/bug/20190607/1193157.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值