简体   繁体   English

如何根据第一个下拉值使第二个下拉列表自动选择值

[英]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.

相关问题 如何根据第一个下拉列表过滤第二个下拉列表 - how to filter 2nd drop-down list according to 1st drop-down list 通过在codeigniter php中使用ajax,根据第一个下拉结果填充第二个下拉列表 - populate 2nd drop down based on 1st drop down result by using ajax in codeigniter php 如何在第二个下拉列表中更改设置的默认值 - How to change the setected default value in a 2nd drop down list 我想做一个条件,如果我从第一个下拉列表中选择这个项目,它会只显示我在第二个下拉列表中选择的项目 - I want to make a condition in which if I select this item from 1st drop down show it shows me only selected items in 2nd drop down 与第二下拉选择相关的第二下拉选择 - 2nd Drop Down selection to be related to the selection on the 1st Drop Down Selection 在第一个下拉菜单被选中后显示第二个下拉菜单的内容。 - Show content of 2nd Drop down after 1st Drop down getting selected. 如何在第一个选择上禁用(或更改颜色)第二个下拉选项? - How to disable (or change color) of 2nd drop down option depenging on 1st selection? 自动 select 下拉值 - Auto select value of drop down 如何基于另一个下拉值选择下拉项 - How to select a drop down item , based on another drop down value 如何基于选择下拉列表 <p> 值 - how to select drop down list based on <p> value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM