【发布时间】:2015-03-20 01:56:59
【问题描述】:
我不懂js。我需要将多选作为逗号从选择框分隔到文本输入中。
使用 pure js,我发现只有这个问题与 SO 相关。 Multiple selections into input box
我尝试了那个问题中的 js 代码。 (问题 js 与已接受答案的组合)
但是我无法实现。
我需要的是例如在选择这 2 个并提交后将 Australia,England 打印到文本输入中。
小提琴链接是:
FIDDLE 中的代码
HTML
<form>
<select id="countries" multiple>
<option value="val0">Australia</option>
<option value="val1">England</option>
<option value="val2">France</option>
</select>
<input type="button" value="Show Index" onclick="showSelected();" />
</form>
<p>selected countries by comma seperated</p>
<form><input type="text" id="txtText" /></form>
不成功的 JS
function showSelected()
{
var selObj = document.getElementById('countries');
var txtTextObj = document.getElementById('txtText');
var selIndex = selObj.selectedIndex;
txtTextObj.value += selObj.options[selIndex].text +', ';
}
【问题讨论】:
-
可以调用JS代码吗?
标签: javascript html textinput multiple-select