获取列表框的显示字符串? [英] Getting a listbox's displayed string?

查看:108
本文介绍了获取列表框的显示字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只想遍历列表框的(多选)项目&对于那些选择了
的人提取显示的文本 - 如下所示 -


函数getSelectedDescrs(lst)

{

var buf;

var maxItems = lst.length;


for(var i = 0; i< maxItems; i ++)

{

if(lst.options [i] .selected == true)

buf + = lst.options [i] .value; //这里有什么?


if(i<(maxItems-1))

buf + ="," ;;

}


返回buf;

}


任何想法


谢谢

解决方案

" harry" < SP *********** @ yahoo.co.uk> skrev i meddelandet

新闻:Rp ***************** @ text.news.blueyonder.co.u k ...

只想遍历列表框''(多选)项目&对于那些选择
的人提取显示的文本 - 如下所示 -

函数getSelectedDescrs(lst)
{var /> var buf;
var maxItems = lst.length;

for(var i = 0; i< maxItems; i ++)
{
if(lst.options [i] .selected == true)
buf + = lst.options [i] .value; //这里有什么?

if(i<(maxItems-1))
buf + ="," ;;
}
返回任何想法

感谢




你想要的.text属性选项。但是为什么不返回一个数组

呢?


函数getSelectedDescrs(lst)

{

var buf = new Array();

var arrIndex = 0;


for(var i = 0,max = lst.length; i< max ; i ++)

{

if(lst.options [i] .selected == true)

buf [arrIndex ++] = lst.options [i] .text;

}


返回buf;

//(返回buf.join(",") ;);会给你确切的结果作为你的原始代码

。但如果这些值碰巧包含,字符会怎么样?)

}


Joakim Braun




harry写道:

Just想要遍历列表框的(多选)项目&对于那些选择的
提取显示的文本 - 如下所示 -

函数getSelectedDescrs(lst)
{var /> var buf;
var maxItems = lst.length;

for(var i = 0; i< maxItems; i ++)
{
if(lst.options [i] .selected == true)
buf + = lst.options [i] .value; //这里有什么

if(i<(maxItems-1))
buf + ="," ;;
}
返回任何想法

感谢




[未经测试]


函数getSelectedDescrs(lst)

{

var o,

buf ='''',
maxItems = lst.options.length;

for(var i = 0; i< maxItems; i ++)

{

if((o = lst.options [i])。selected)

buf + = o.text +'','';

}

返回buf.replace(/,


/,'''');

}


Just want to traverse a listbox''s (multi select) items & for those that are
selected extract the displayed text - something like below -

function getSelectedDescrs(lst)
{
var buf;
var maxItems = lst.length;

for (var i=0; i < maxItems; i++)
{
if (lst.options[i].selected == true)
buf+=lst.options[i].value; // What goes here?

if(i<(maxItems-1))
buf+=",";
}

return buf;
}

any ideas

thanks

解决方案

"harry" <sp***********@yahoo.co.uk> skrev i meddelandet
news:Rp*****************@text.news.blueyonder.co.u k...

Just want to traverse a listbox''s (multi select) items & for those that are selected extract the displayed text - something like below -

function getSelectedDescrs(lst)
{
var buf;
var maxItems = lst.length;

for (var i=0; i < maxItems; i++)
{
if (lst.options[i].selected == true)
buf+=lst.options[i].value; // What goes here?

if(i<(maxItems-1))
buf+=",";
}

return buf;
}

any ideas

thanks



You want the .text property of the option. But why not return an array
instead?

function getSelectedDescrs(lst)
{
var buf = new Array();
var arrIndex = 0;

for (var i=0, max = lst.length; i < max; i++)
{
if (lst.options[i].selected == true)
buf[arrIndex++] = lst.options[i].text;
}

return buf;
// (return buf.join(","); would give you the exact result as your
original code. But what if the values happen to contain "," characters?)
}

Joakim Braun



harry wrote:

Just want to traverse a listbox''s (multi select) items & for those that are selected extract the displayed text - something like below -

function getSelectedDescrs(lst)
{
var buf;
var maxItems = lst.length;

for (var i=0; i < maxItems; i++)
{
if (lst.options[i].selected == true)
buf+=lst.options[i].value; // What goes here?
if(i<(maxItems-1))
buf+=",";
}

return buf;
}

any ideas

thanks



[untested]

function getSelectedDescrs(lst)
{
var o,
buf = '''',
maxItems = lst.options.length;
for (var i = 0; i < maxItems; i++)
{
if ((o = lst.options[i]).selected)
buf += o.text + '', '';
}
return buf.replace(/,


/, '''');
}


这篇关于获取列表框的显示字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆