[英]How to make 2nd drop down list auto select value based on 1st drop down value
I have two drop down list, one for Client and Location.我有两个下拉列表,一个用于客户和位置。 I would like the location drop down to auto select a value based on the selected Client option.
我希望位置下拉菜单根据选定的客户端选项自动选择一个值。
This is the script i came up with but the value is not being displayed on the location drop down.这是我想出的脚本,但该值未显示在位置下拉列表中。
function defaultLocation () {
var client2 = document.getElementById('clientList2');
if (client2.value == "Arm") {
document.getElementById('locationList2').value == "Cambridge";
}
}
As of now, I want the value to be auto selected when the Client option is chosen with no need to click a button.截至目前,我希望在选择 Client 选项时自动选择该值,而无需单击按钮。
UPDATE: I would still like the user to be able to choose something else from the location list if the default option is not wanted?更新:如果不需要默认选项,我仍然希望用户能够从位置列表中选择其他内容吗? How would i do that as of right now, once "Arm" is selected, i can't change the location option
我现在该怎么做,一旦选择了“手臂”,我就无法更改位置选项
To assign value, you have to use assignment operator ( =
) not ==
:要赋值,您必须使用赋值运算符 (
=
) 而不是==
:
document.getElementById('locationList2').value = "Cambridge";
once "Arm" is selected, i can't change the location option
选择“手臂”后,我无法更改位置选项
But I am unable to raise the issue in the following:但我无法提出以下问题:
function defaultLocation () { var client2 = document.getElementById('clientList2'); if (client2.value == "Arm") { document.getElementById('locationList2').value = "Cambridge"; } }
<select id="clientList2" onchange="defaultLocation()"> <option value="c1">Client 1</option> <option value="Arm">Arm</option> </select> <select id="locationList2"> <option value="l1">Location 1</option> <option value="Cambridge">Cambridge</option> </select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.