你可以這樣做。我已經做了一些修改上ADDID和寫hideId
var select_count = 0;
function hideId()
{
var selectedIndexes = [];
for(var i=0; i<select_count; i++)
{
var select = document.getElementById("select-" + i);
for(var j=0; j<select.options.length; j++)
{
select.options[j].disabled = false;
}
for(var j=0; j<selectedIndexes.length; j++)
{
var index = selectedIndexes[j];
select.options[index].disabled = true;
if(select.selectedIndex == index)
{
select.selectedIndex = 0;
}
}
selectedIndexes.push(select.selectedIndex);
}
}
function addId() {
var location = document.getElementById("place");
var div = document.createElement("div");
var span = document.createElement("span");
var select = document.createElement("select");
select.setAttribute("id","select-" + select_count);
select_count++;
select.setAttribute("onchange","hideId()");
var option0 = document.createElement("option");
option0.setAttribute("id","option0");
option0.innerHTML="select";
select.appendChild(option0);
var option1 = document.createElement("option");
option1.setAttribute("id","option1");
option1.innerHTML="aaaa";
select.appendChild(option1);
var option2 = document.createElement("option");
option2.innerHTML="bbbb";
select.appendChild(option2);
var option3 = document.createElement("option");
option3.innerHTML="cccc";
select.appendChild(option3);
span.appendChild(select);
div.appendChild(span);
location.appendChild(div);
if(select_count > 0)
hideId();
}
UPDATE:
如果你需要它與n個元素的工作,你能做到這樣。 要添加更多元素,只需將它們添加到select_values。 此外,我已修改hideId因此它不會隱藏第一個值(「選擇」)。
var select_count = 0;
var select_values = ["select", "aaa", "bbb", "ccc", "ddd", "eee"];
function hideId()
{
var selectedIndexes = [];
for(var i=0; i<select_count; i++)
{
var select = document.getElementById("select-" + i);
for(var j=0; j<select.options.length; j++)
{
select.options[j].disabled = false;
}
for(var j=0; j<selectedIndexes.length; j++)
{
var index = selectedIndexes[j];
select.options[index].disabled = true;
if(select.selectedIndex == index)
{
select.selectedIndex = 0;
}
}
if(select.selectedIndex != 0)
selectedIndexes.push(select.selectedIndex);
}
}
function addOption(select, id, value)
{
var option1 = document.createElement("option");
option1.setAttribute("id", id);
option1.innerHTML = value;
select.appendChild(option1);
}
function addId()
{
var location = document.getElementById("place");
var div = document.createElement("div");
var span = document.createElement("span");
var select = document.createElement("select");
select.setAttribute("id","select-" + select_count);
select_count++;
select.setAttribute("onchange","hideId()");
for(var i=0; i<select_values.length; i++)
addOption(select, "option" + i, select_values[i]);
span.appendChild(select);
div.appendChild(span);
location.appendChild(div);
if(select_count > 0)
hideId();
}
更新2:
以任何順序工作(不只是從倒置),使用此hideId()版本:
function hideId()
{
var selectedIndexes = [];
for(var i=0; i<select_count; i++)
{
var select = document.getElementById("select-" + i);
for(var j=0; j<select.options.length; j++)
{
select.options[j].disabled = false;
}
if(select.selectedIndex != 0)
selectedIndexes.push({ index: select.selectedIndex, select: select });
}
for(var i=0; i<select_count; i++)
{
var select = document.getElementById("select-" + i);
for(var j=0; j<selectedIndexes.length; j++)
{
var selected = selectedIndexes[j];
if(selected.select != select)
{
select.options[selected.index].disabled = true;
if(select.selectedIndex == selected.index)
select.selectedIndex = 0;
}
}
}
}
如果我理解正確,你需要任何選擇來包含除前面選擇中選擇的所有選項? – Pato
「',當我將第一個選擇選項從aaa更改爲bbb時,aaa選項應該再次出現在選擇框中」「,而」bbb「應該從第二個選擇框中消失? – ted
看看[這個問題](http://stackoverflow.com/questions/12422854/dropdown-box-with-expanding-optgroup/12425609#12425609) – noel