gpt4 book ai didi

javascript - javascript中的条件下拉菜单,选择器问题

转载 作者:太空宇宙 更新时间:2023-11-03 18:13:49 25 4
gpt4 key购买 nike

我有一个有效的条件下拉菜单,但这取决于我将其放在页面上的位置。如果我不把它放在特定位置,菜单中的位置根本不会填满。问题还在于我想添加第二个条件下拉菜单,无论我把它放在哪里,它都不起作用。所以我的问题是:我是否需要在页面上添加两次脚本以对应我拥有的两种形式?我实际上希望两个下拉菜单都填充完全相同的词。

document.forms[0]['List'+i].length = 1;
document.forms[0]['List'+i].selectedIndex = 0;

我猜它与那里的 [0] 有关。我尝试向表单添加一个 id,然后编写第二个脚本 document.forms('myId')[] 但这也不起作用。我应该怎么做?

<script type="text/javascript">

var categories = [];
categories["startList"] = ["Programming","Science","History","Business and Economics","Software","Languages","Do it Yourself","Others"];
categories["Programming"] = ["Java","C++","C.","Python","Html","Php","Mysql","ObjectiveC","Android","Others"];
categories["Science"] = ["Mathematics","Physics","Biology","Chemistry","Medicine","Astronomy","Statistics","Others"];
categories["Others"] = ["All"]

var nLists = 2; // number of lists in the set

function fillSelect(currCat,currList){
var step = Number(currList.name.replace(/\D/g,""));
for (i=step; i<nLists+1; i++) {
document.forms[0]['List'+i].length = 1;
document.forms[0]['List'+i].selectedIndex = 0;
}
var nCat = categories[currCat];
for (each in nCat) {
var nOption = document.createElement('option');
var nData = document.createTextNode(nCat[each]);
nOption.setAttribute('value',nCat[each]);
nOption.appendChild(nData);
currList.appendChild(nOption);
}
}

function getValue( L2, L1) {
$.post( "", { List1: L1, List2: L2 } );
}

function init() {
fillSelect('startList',document.forms[0]['List1'])
}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);

</script>




<form action="" method="post">
<select name='List1' onchange="fillSelect(this.value,this.form['List2'])">
<option selected>Category</option>
</select>

&nbsp;
<select name='List2' onchange="getValue(this.value,this.form['List1'].value)">
<option selected >Subcategory</option>
</select>
<input type="Submit">

最佳答案

修复它。我只需要添加一个选择器或 1 来对应我的第二种形式。

 document.forms[1]['List'+i].length = 1;
document.forms[1]['List'+i].selectedIndex = 0;
}
var nCat = categories[currCat];
for (each in nCat) {
var nOption = document.createElement('option');
var nData = document.createTextNode(nCat[each]);
nOption.setAttribute('value',nCat[each]);
nOption.appendChild(nData);
currList.appendChild(nOption);
}
}

function getValue( L2, L1) {
$.post( "", { List1: L1, List2: L2 } );
}

function init() {
fillSelect('startList',document.forms[1]['List1'])

关于javascript - javascript中的条件下拉菜单,选择器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23163706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com