在下面提到的代码上需要专家的意见,我不擅长javascript。请帮忙

我需要的是,我有2个下拉菜单。一个是catid1,第二个是catid ...
catid从数据库中加载一些值,并且catid1有一些手动输入项,它们几乎相同,而很少不同。但是我想要的是当我从“ abc”之类的列表中选择catid1值时,catid值应更改为与catid1“ abc”相同
我尝试了一些不同的方法,但无法解决此问题,任何人都建议这可能与否。

<div class="col-md-4">
    <div class="form-group">
        <label for="catId"><?php echo $categoryField; ?></label>
        <select class="form-control" name="catId1" id="catId1" onChange="qt();">
            <option> Select Base Query Type </option>
        </select>
        <span class="help-block"><?php echo $categoryHelp; ?></span>
    </div>
</div>
</div>

<div class="col-md-4">
    <div class="form-group">
    <label for="catId"><?php echo $categoryField; ?></label>
    <select class="form-control" name="catId" id="catId">
        <?php
        $tcat = "SELECT catId, catName FROM categories WHERE userId = ".$userId." AND isActive = 1  ";
        $rest = mysqli_query($mysqli, $tcat) or die('-2'.mysqli_error());
        ?>
        <option value="..."><?php echo $selectOption; ?></option>
        <?php while ($tcatrow = mysqli_fetch_assoc($rest)) { ?>
        <option value="<?php echo $tcatrow['catId']; ?>"><?php echo clean($tcatrow['catName']); ?></option>
        <?php } ?>
    </select>
    <span class="help-block"><?php echo $categoryHelp; ?></span>
    </div>
</div>
</div>


我尝试过的示例代码也如下,但在我的情况下不起作用。

<select name="dropdown[]">
  <option value="1">Sony</option>
  <option value="2">Nintendo</option>
  <option value="3">Microsoft</option>
</select>

<select name="dropdown[]">
  <option value="1">Sony</option>
  <option value="2">Nintendo</option>
  <option value="3">Microsoft</option>
</select>

<select name="dropdown[]">
  <option value="1">Sony</option>
  <option value="2">Nintendo</option>
  <option value="3">Microsoft</option>
</select>

var selects = document.querySelectorAll('select[name="dropdown[]"]');

selects[0].addEventListener('change', function () {
    for (var i = 0; i < selects.length; i++) {
        selects[i].value = selects[0].value;
    }
});

最佳答案

您在第二个示例中忘记了脚本标记:

<select name="dropdown[]">
   <option value="1">Sony</option>
   <option value="2">Nintendo</option>
   <option value="3">Microsoft</option>
</select>

<select name="dropdown[]">
  <option value="1">Sony</option>
  <option value="2">Nintendo</option>
  <option value="3">Microsoft</option>
</select>

<select name="dropdown[]">
  <option value="1">Sony</option>
  <option value="2">Nintendo</option>
  <option value="3">Microsoft</option>
</select>

<script language="javascript">
    var selects = document.querySelectorAll('select[name="dropdown[]"]');
  selects[0].addEventListener('change', function () {
    for (var i = 0; i < selects.length; i++) {
      selects[i].value = selects[0].value;
    }
  });
</script>


jsfiddle:https://jsfiddle.net/ge127u8d/

09-30 23:31