使用表单中的javascript添加到多个选择列表
我想有2个空白的输入表单-类别和值,当按下一个按钮时,它们被附加/添加到2个多选择表单中,类别和值。按下按钮时,未输入数据使用表单中的javascript添加到多个选择列表,javascript,html,forms,select,Javascript,Html,Forms,Select,我想有2个空白的输入表单-类别和值,当按下一个按钮时,它们被附加/添加到2个多选择表单中,类别和值。按下按钮时,未输入数据 function doAdd() { // Pick up data from the category and value input fields; // In my form these are named 'cat' and 'val' var catstr = document.getElementById("ca
function doAdd() {
// Pick up data from the category and value input fields;
// In my form these are named 'cat' and 'val'
var catstr = document.getElementById("cat").value;
var valstr = document.getElementById("val").value;
// pick up references to the text areas;
var cats = document.getElementById("catlist");
var nums = document.getElementById("numlist");
// Append text, inserting a new line character between
// data sets.
if (numadded > 0) {
cats.value = cats.value + "\n";
nums.value = nums.value + "\n";
}
numadded++;
cats.value = cats.value + catstr;
nums.value = nums.value + valstr;
}
HTML重要行
<script type="text/javascript" src="./checksubmit.js" ></script>
<input type="text" id="val" name="val" size="10"/>
<input type="text" id="cat" name="cat" size="30"/>
<input type="button" onclick="doAdd();" value="Add item">
<select multiple="multiple" id="catlist" style="width: 250px;"/>
<select multiple="multiple" id="numlist" style="width: 250px;"/>
我相信你想要这样的东西
function doAdd() {
// Pick up data from the category and value input fields;
// In my form these are named 'cat' and 'val'
var catstr = document.getElementById("cat").value;
var valstr = document.getElementById("val").value;
// pick up references to the text areas;
var cats = document.getElementById("catlist");
var nums = document.getElementById("numlist");
//Create and append new options
var catOption = new Option(catstr, valstr);
var numOption = new Option(valstr, valstr);
cats.appendChild(catOption);
nums.appendChild(numOption);
}
你想在你的选择中添加新选项吗?是的,这正是我想做的。请发布你的答案,而不是我需要的课本解决方法,但这将完成工作。格雷西亚斯!