选择下拉选项时如何触发javascript

时间:2020-12-04 15:08:32

I have two dropdown boxes for car makes and models, when a make is selected i need all its models to show in the next box. As of now using the following code the models show up when the dropdown is clicked.

我有两个用于汽车品牌和型号的下拉框,当选择一个品牌时,我需要在下一个盒子中显示所有型号。截至目前,使用以下代码时,模型会在单击下拉列表时显示。

    window.onmousedown = function(e){
    this.id = e.target.id;
    if(this.id == "vehicle_makes"){
        var make = document.getElementById("vehicle_makes").value;
        ajaxCall(make);
    }
}

However i need the javascript to trigger when an option from the dropdown is selected rather than when the drop down is opened.

但是,当选择下拉列表中的选项而不是打开下拉列表时,我需要触发javascript。

here also is the html - with some php

这里也是html - 有一些PHP

<div class="vehicle-search-wrap">
    <form method="get" id="searchform" action="<?php bloginfo('url'); ?>">
        <div>
            <select id="vehicle_makes" name="s">
            <?php
                foreach($makes as $make){ //put all makes in dropdown
                    echo "<option value='". $make ."'>". $make ."</option>";
                }
            ?>
            </select>
            <select id="model_drop" name="vmodels">
            <?php
            //nothing to start
            ?>
            </select>       
            <input type="submit" value="Search Vehicle" />
        </div>
    </form>
</div>

2 个解决方案

#1


5  

use the onchange event of selectbox.

使用selectbox的onchange事件。

here is a demo

这是一个演示

#2


0  

Try this:

尝试这个:

<select name="Pizza" size="5"
    onchange="myFunctionHere()">
  <option value="P101">Pizza Napoli</option>
  <option value="P102">Pizza Roma</option>
</select>

#1


5  

use the onchange event of selectbox.

使用selectbox的onchange事件。

here is a demo

这是一个演示

#2


0  

Try this:

尝试这个:

<select name="Pizza" size="5"
    onchange="myFunctionHere()">
  <option value="P101">Pizza Napoli</option>
  <option value="P102">Pizza Roma</option>
</select>