如何在另一个选择列表的更改功能上更改选择列表的值?

时间:2019-03-21 09:04:34

标签: javascript jquery html asp.net-mvc

我有这个select并且绑定了更改事件。我要在其中的值的change上选择另一个选择列表中的特定元素,即其中的特定选项。

但是它不起作用。主服务中有些服务不需要选择子服务,因此应选择默认的-select-

$("#MainServices").change(function() {
  $(#SubServices).val("-Select-")
}
<select class="d-flex flex-wrap" name="MainServiceName" id="MainServices">
  <option selected value="m-None">-Select-</option>
  <option value="m-SS">Software Services</option>
  <option value="m-AIMS">Asset Integrity Management Services</option>
  <option value="m-HSE">HSE & Environmental Services</option>
  <option value="m-ES">Engineering Services</option>
</select>

<select class="d-flex flex-wrap" name="SubServiceName" id="SubServices">
  <option class="" selected value="s-None">-Select-</option>
  <option class="SoftwareServices" value="s-AIMS">VAIL-Plant (Asset Integrity Management System)</option>
  <option class="SoftwareServices" value="s-PHA">VAIL-PHA (Process Hazard Analysis)</option>
  <option class="SoftwareServices" value="s-PSRA">VAIL-PSRA (Petrol Station Risk Assessment)</option>
  <option class="SoftwareServices" value="s-MTS">VAIL-MTS (Material Tracking System)</option>
</select>

1 个答案:

答案 0 :(得分:0)

-Select-是该选项的文本,值为s-None,因此应为

$("#MainServices").change(function() {
  $("#SubServices").val("s-None")
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="d-flex flex-wrap" name="MainServiceName" id="MainServices">
  <option selected value="m-None">-Select-</option>
  <option value="m-SS">Software Services</option>
  <option value="m-AIMS">Asset Integrity Management Services</option>
  <option value="m-HSE">HSE & Environmental Services</option>
  <option value="m-ES">Engineering Services</option>
</select>

<select class="d-flex flex-wrap" name="SubServiceName" id="SubServices">

  <option class="" selected value="s-None">-Select-</option>
  <option class="SoftwareServices" value="s-AIMS">VAIL-Plant (Asset Integrity Management System)</option>
  <option class="SoftwareServices" value="s-PHA">VAIL-PHA (Process Hazard Analysis)</option>
  <option class="SoftwareServices" value="s-PSRA">VAIL-PSRA (Petrol Station Risk Assessment)</option>
  <option class="SoftwareServices" value="s-MTS">VAIL-MTS (Material Tracking System)</option>
</select>