javascript JS和HTML选择框,根据用户的首次选择预先选择值

w8f9ii69  于 2023-06-20  发布在  Java
关注(0)|答案(3)|浏览(113)

我正在使用HTML,JS和Python开发一个WebApp,其想法是有一些选择框,用户选择第一个,并基于此在后端预先选择其他人。
规则如下:

  • 如果用户选择A,则选择Level 3
  • 如果用户选择B,则选择级别0
  • 如果用户选择C,则仅显示级别4和级别5

我开始做这样的事情:

const DGSelect = document.getElementById('DG');
const STGSelect = document.getElementById('STG');

STGSelect.value = '';
DGSelect.value = '';

function preselectSTG() {
  const selectedDGValue = DGSelect.value;

  if (selectedDGValue === 'A') {
    STGSelect.value = 'LVL 3';
  } else if (selectedDGValue === 'B') {
    STGSelect.value = 'LVL 0';
  } else if (selectedDGValue === 'C') {
    STGSelect.value = 'LVL 4';
  } else {
    STGSelect.value = '';
    DGSelect.value = '';
  }
}
<form>
  <div class="form-group">
    <label for="DG">Decision Group:</label>
    <select name="DG" id="DG">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">C</option>
    </select>
  </div>

  <div class="form-group">
    <label for="STG">STGCD:</label>
    <select name="STG" id="STG">
      <option value="LVL 0">Level 0</option>
      <option value="LVL 3">Level 3</option>
      <option value="LVL 4">Level 4</option>
      <option value="LVL 5">Level 5</option>
    </select>
  </div>
</form>
lmyy7pcs

lmyy7pcs1#

我不知道这是否有帮助,我只是放置了一个事件侦听器,使其与元素交互。
你写一个关于什么是失踪下面的评论,我会尽我所能把代码工作所需。

const DGSelect = document.getElementById('DG');
const STGSelect = document.getElementById('STG');

STGSelect.value = '';
DGSelect.value = '';

DGSelect.addEventListener("change", preselectSTG)
function preselectSTG() {
  const selectedDGValue = DGSelect.value;

  if (selectedDGValue === 'A') {
    STGSelect.value = 'LVL 3';
  } else if (selectedDGValue === 'B') {
    STGSelect.value = 'LVL 0';
  } else if (selectedDGValue === 'C') {
    STGSelect.value = 'LVL 4';
  } else {
    STGSelect.value = '';
    DGSelect.value = '';
  }
}
<form>
  <div class="form-group">
    <label for="DG">Decision Group:</label>
    <select name="DG" id="DG">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">C</option>
    </select>
  </div>

  <div class="form-group">
    <label for="STG">STGCD:</label>
    <select name="STG" id="STG">
      <option value="LVL 0">Level 0</option>
      <option value="LVL 3">Level 3</option>
      <option value="LVL 4">Level 4</option>
      <option value="LVL 5">Level 5</option>
    </select>
  </div>
</form>
puruo6ea

puruo6ea2#

首先,您需要一个调用该函数的事件侦听器。
其次,函数缺少第三种情况,隐藏了级别4和5。

const DGSelect = document.getElementById('DG');
const STGSelect = document.getElementById('STG');

STGSelect.value = '';
DGSelect.value = '';

DGSelect.addEventListener("change", preselectSTG);

function preselectSTG() {
  const selectedDGValue = DGSelect.value;
  // Preselect option from STG
  if (selectedDGValue === 'A') {
    STGSelect.value = 'LVL 3';
  } else if (selectedDGValue === 'B') {
    STGSelect.value = 'LVL 0';
  } else if (selectedDGValue === 'C') {
    STGSelect.value = 'LVL 4';
  } else {
    STGSelect.value = '';
    DGSelect.value = '';
  }
  // Hide unavailable options in STG
  if (selectedDGValue === 'C') {
    document.getElementById("STG0").style.display = 'none';
    document.getElementById("STG3").style.display = 'none';
  } else {
    document.getElementById("STG0").style.display = 'block';
    document.getElementById("STG3").style.display = 'block';
  }
}
<form>
  <div class="form-group">
    <label for="DG">Decision Group:</label>
    <select name="DG" id="DG">
      <option value="">--Select--</option>
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
    </select>
  </div>

  <div class="form-group">
    <label for="STG">STGCD:</label>
    <select name="STG" id="STG">
      <option value="">--Select--</option>
      <option value="LVL 0" id="STG0">Level 0</option>
      <option value="LVL 3" id="STG3">Level 3</option>
      <option value="LVL 4" id="STG4">Level 4</option>
      <option value="LVL 5" id="STG5">Level 5</option>
    </select>
  </div>
</form>
zsohkypk

zsohkypk3#

首先在这个选择部分

<select name="DG" id="DG">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">C</option>
    </select>

我想你是说最后一个选项,<option value="D">D</option>
则需要在first select部分**(id=“DG”)中添加onchange选项**,如下所示

<select name="DG" id="DG" onchange="preselectSTG()">
     <option value="A">A</option>
     <option value="B">B</option>
     <option value="C">C</option>
     <option value="D">D</option>
</select>

对于第二个div****(id=“STG”),应该是这样的:

<div class="form-group">
      <label for="STG">STGCD:</label>
      <select name="STG" id="STG"></select>
    </div>

那么对于Java脚本代码,代码如下:JS

const DGSelect = document.getElementById("DG");
    const STGSelect = document.getElementById("STG");
    var op = document.createElement("option"); // creats a new option element
    STGSelect.disabled = true; // disable the second select
    
    function preselectSTG() {
      const selectedDGValue = DGSelect.value;
      STGSelect.disabled = false; // the disable is set to false
      if (selectedDGValue === "A") {
        STGSelect.innerHTML = ""; //clear all options in second Select
        op.textContent = "Level 3"; //the text between option tag
        op.value = "LVL 3"; // the value of option
        STGSelect.appendChild(op); // add the element to the select
      } else if (selectedDGValue === "B") {
        STGSelect.innerHTML = "";
        op.textContent = "Level 0";
        op.value = "LVL 0";
        STGSelect.appendChild(op);
      } else if (selectedDGValue === "C") {
        STGSelect.innerHTML = ""; 
        op.textContent = "Level 4"; // add the lvl 4
        op.value = "LVL 4";
        STGSelect.appendChild(op);
        var op2 = document.createElement("option");
        op2.textContent = "Level 5"; //add the lvl 5
        op2.value = "LVL 5";
        STGSelect.appendChild(op2);
      } else {
        STGSelect.innerHTML = "";
        op.textContent = "";
        op.value = "";
      }

我强烈建议寻找createElement方法,这是非常有用的,希望这有帮助

相关问题