添加几个<;选项>;来自a中的对象<;选择>;使用javascript
我正在尝试用两个select构建一个表单。根据第一个选项中的选择,第二个选项中的可能选项列表应进行调整 所以,如果选择1,则选择值为1=>将对象1显示为选择2中的选项 我创建了这个js代码:添加几个<;选项>;来自a中的对象<;选择>;使用javascript,javascript,object,select,Javascript,Object,Select,我正在尝试用两个select构建一个表单。根据第一个选项中的选择,第二个选项中的可能选项列表应进行调整 所以,如果选择1,则选择值为1=>将对象1显示为选择2中的选项 我创建了这个js代码: var EntiteGenerale = document.getElementById("TypeEntityId").value; var select = document.getElementById("EntityId"); var index; var Departement = { 1:
var EntiteGenerale = document.getElementById("TypeEntityId").value;
var select = document.getElementById("EntityId");
var index;
var Departement = {
1: "Finances et Administration",
2: "Service Logistique Transversale",
3: "Institut de Formation",
4: "Communication, Marketing & Commercial",
5: "Volontariat",
6: "Ressources Humaines",
7: "Service francophone du sang",
8: "Service ECM et DIH",
9: "Service Tracing",
10: "Département Secours"
};
var CentreSecours = {
1: "CG BUTGENBACH BULLINGEN",
2: "CS BLEGNY",
3: "CS BRABANT OUEST",
4: "CS CHARLEROI",
5: "CS HAUTE SENNE",
6: "CS HERSTAL - OUPEYE",
7: "CS TOURNAI",
8: "CS NAMUR",
9: "CS OTTIGNIES",
10: "CS OUGREE",
11: "CS PHILIPPEVILLE",
12: "CS ROCHEFORT",
13: "CS SPA",
14: "CS HESBAYE-CONDROZ",
15: "CS JODOIGNE",
16: "CS LIEGE",
17: "CS LUXEMBOURG",
18: "CS MONS",
19: "CS MOUSCRON"
};
function DisplayEntiteL() {
if (EntiteGenerale === 2) {
for (index in Departement) {
select.options[select.options.length] = new Option(Departement[index], index);
}
} else if (EntiteGenerale === 3) {
for (index in CentreSecours) {
select.options[select.options.length] = new Option(CentreSecours[index], index);
}
} else {
console.log("rien à afficher");
}
}
但当我尝试时,else语句“rienáafficher”总是显示出来,因为我的选项没有添加
你能告诉我我的密码有什么问题吗?
请参阅本文中的完整代码
先谢谢你
致以最良好的祝愿
int
。否则===
将返回false。因为==
还将验证数据类型var EntiteGenerale=document.getElementById(“TypeEntityId”);
var select=document.getElementById(“EntityId”);
var指数;
var部门={
1.“财政和行政管理”,
2:“横向服务物流”,
3.“信息机构”,
4:“传播、营销和商业”,
5:“沃伦塔里亚人”,
6.“人类资源”,
7:“法语国家杜桑服务”,
8:“维修ECM和DIH”,
9:“服务追踪”,
10.“分手的秘密”
};
var CentreSecours={
1:“CG BUTGENBACH BULLINGEN”,
2:“CS BLEGNY”,
3:“CS BRABANT OUEST”,
4:“CS CHARLEROI”,
5:“高级警司”,
6:“CS HERSTAL-OUPEYE”,
7:“CS图尔奈”,
8:“CS NAMUR”,
9:“CS OTTIGNIES”,
10:“CS OUGREE”,
11:"飞利浦维尔",,
12:“CS ROCHEFORT”,
13.“CS SPA”,
14:“CS HESBAYE-CONDROZ”,
15:“CS JODOIGNE”,
16:“CS列日”,
17:“CS卢森堡”,
18.“CS MONS”,
19:“CS MOUSCRON”
};
函数displayentel(){
EntiteGenerale=parseFloat(EntiteGenerale.value);
如果(EntiteGenerale==2){
用于(部门索引){
select.options[select.options.length]=新选项(department[index],index);
}
}else if(EntiteGenerale==3){
用于(中心课程索引){
select.options[select.options.length]=新选项(CentreSecours[index],index);
}
}否则{
console.log(“rienáafficher”);
}
}
input[type='checkbox']{
-webkit外观:无;
-moz外观:无;
-o-外观:无;
边框:1px实心rgba(0,0,0,0.1);
宽度:15px;
高度:15px;
-webkit边界半径:3px;
-moz边界半径:3px;
边界半径:3px;
盒影:插入0 1px 1px rgba(0,0,0,075);
-webkit过渡:边框颜色缓进缓出.15s,-webkit盒阴影缓进缓出.15s;
-o型过渡:边框颜色缓进缓出.15s,框阴影缓进缓出.15s;
过渡:边框颜色淡入淡出.15s,框阴影淡入淡出.15s;
}
输入[type='radio']{
-webkit外观:无;
-moz外观:无;
-o-外观:无;
边框:1px实心rgba(0,0,0,0.1);
宽度:15px;
高度:15px;
-webkit边界半径:3px;
-moz边界半径:3px;
边界半径:3px;
盒影:插入0 1px 1px rgba(0,0,0,075);
-webkit过渡:边框颜色缓进缓出.15s,-webkit盒阴影缓进缓出.15s;
-o型过渡:边框颜色缓进缓出.15s,框阴影缓进缓出.15s;
过渡:边框颜色淡入淡出.15s,框阴影淡入淡出.15s;
}
输入[type='radio']:已选中{
背景色:红色;
}
输入[type='checkbox']:选中{
背景色:红色;
}
输入::占位符{
字体大小:0.7rem;
}
挑选{
边界半径:20px;
字体大小:0.8rem;
}
选择:焦点{
边框颜色:#ff2400;
大纲:0;
盒影:无;
}
最后一句话:
选举
Départments&;服务中心
第二中心
区域设置(Bruxelles)
省委员会
雷莱斯
西苏
艾达中心
梅森克罗伊胭脂
Votre entité地区:
选择地点
如果我正确理解了您的问题,您希望EntityId
选择的
元素根据类型EntityId
选择元素的值动态更改。考虑修改您的代码,使<代码>更改< /C>事件被添加到<代码> TypeEntityId <代码> >通过<代码> AdvestEntListNEnter()/代码>:
此外,还可以修改if
行,以便比较中的值是字符串,以确保可以满足值匹配条件:
/* Add event parameter to localize value extraction from corresponding
select element */
function DisplayEntiteL(event) {
/* Get current value of TypeEntityId select */
const value = event.currentTarget.value
const entityId = document.getElementById("EntityId");
/* Empty any previous options from EntityId select */
while (entityId.hasChildNodes()) {
entityId.removeChild(entityId.lastChild);
}
/* If value of TypeEntityId is 2 then show Departement items in
entityId select */
if (value === '2') {
for (index in Departement) {
entityId.appendChild(new Option(Departement[index], index));
}
}
/* If value of TypeEntityId is 3 then show CentreSecours items in
entityId select */
else if (value === '3') {
for (index in CentreSecours) {
entityId.appendChild(new Option(CentreSecours[index], index));
}
}
/* Otherwise log to console */
else {
/* entityId.appendChild(new Option('No options')); */
console.log("rien à afficher");
}
}
此外,上面的代码引入了事件
参数,允许您在事件处理程序中访问TypeEntityId
选择的值。有关完整的工作示例或下面的代码段:
var EntiteGenerale=document.getElementById(“TypeEntityId”).value;
var部门={
1.“财政和行政管理”,
2:“横向服务物流”,
3.“信息机构”,
4:“传播、营销和商业”,
5:“沃伦塔里亚人”,
6.“人类资源”,
7:“法语国家杜桑服务”,
8:“维修ECM和DIH”,
9:“服务追踪”,
10.“分手的秘密”
};
var CentreSecours={
1:“CG BUTGENBACH BULLINGEN”,
2:“CS BLEGNY”,
3:“CS BRABANT OUEST”,
4:“CS CHARLEROI”,
5:“高级警司”,
6:“CS HERSTAL-OUPEYE”,
7:“CS图尔奈”,
8:“CS NAMUR”,
9:“CS OTTIGNIES”,
10:“CS OUGREE”,
11:"飞利浦维尔",,
12:“CS ROCHEFORT”,
13.“CS SPA”,
14:“CS HESBAYE-CONDROZ”,
15:“CS JODOIGNE”,
16:“CS列日”,
17:“CS卢森堡”,
18.“CS MONS”,
19:“CS MOUSCRON”
};
/*添加事件参数以本地化从相应
选择元素*/
功能显示电话(事件){
/*获取TypeEntityId选择的当前值*/
常量值=event.currentTarget.value
const entityId=document.getElementById(“
/* Add event parameter to localize value extraction from corresponding
select element */
function DisplayEntiteL(event) {
/* Get current value of TypeEntityId select */
const value = event.currentTarget.value
const entityId = document.getElementById("EntityId");
/* Empty any previous options from EntityId select */
while (entityId.hasChildNodes()) {
entityId.removeChild(entityId.lastChild);
}
/* If value of TypeEntityId is 2 then show Departement items in
entityId select */
if (value === '2') {
for (index in Departement) {
entityId.appendChild(new Option(Departement[index], index));
}
}
/* If value of TypeEntityId is 3 then show CentreSecours items in
entityId select */
else if (value === '3') {
for (index in CentreSecours) {
entityId.appendChild(new Option(CentreSecours[index], index));
}
}
/* Otherwise log to console */
else {
/* entityId.appendChild(new Option('No options')); */
console.log("rien à afficher");
}
}
<select onchange="DisplayEntiteL(event)"></select>
var EntiteGenerale = document.getElementById("TypeEntityId");
var select = document.getElementById("EntityId");
var index;
var Departement = {
1: "Finances et Administration",
2: "Service Logistique Transversale",
3: "Institut de Formation",
4: "Communication, Marketing & Commercial",
5: "Volontariat",
6: "Ressources Humaines",
7: "Service francophone du sang",
8: "Service ECM et DIH",
9: "Service Tracing",
10: "Département Secours"
};
var CentreSecours = {
1: "CG BUTGENBACH BULLINGEN",
2: "CS BLEGNY",
3: "CS BRABANT OUEST",
4: "CS CHARLEROI",
5: "CS HAUTE SENNE",
6: "CS HERSTAL - OUPEYE",
7: "CS TOURNAI",
8: "CS NAMUR",
9: "CS OTTIGNIES",
10: "CS OUGREE",
11: "CS PHILIPPEVILLE",
12: "CS ROCHEFORT",
13: "CS SPA",
14: "CS HESBAYE-CONDROZ",
15: "CS JODOIGNE",
16: "CS LIEGE",
17: "CS LUXEMBOURG",
18: "CS MONS",
19: "CS MOUSCRON"
};
function clearOption(){
var x = document.getElementById("EntityId");
while (x.length != 0) {
x.remove(x.length-1);
}
}
function DisplayEntiteL(event) {
if (event.target.value === '1') {
clearOption();
for (index in Departement) {
select.options[select.options.length] = new Option(Departement[index], index);
}
} else if (event.target.value === '2') {
clearOption();
for (index in CentreSecours) {
select.options[select.options.length] = new Option(CentreSecours[index], index);
}
} else {
console.log("rien à afficher");
}
}