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.截至目前,使用以下代码,单击下拉菜单时会显示模型。
window.onmousedown = function(e){
this.id = e.target.id;
if(this.id == "vehicle_makes"){
var make = document.getElementById("vehicle_makes").value;
ajaxCall(make);
}
}
但是,我需要在从下拉菜单中选择一个选项时触发 javascript,而不是在打开下拉菜单时触发。
这里还有 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>
最佳答案
使用选择框的onchange
事件。
这是一个demo
关于javascript - 选择下拉选项时如何触发javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17921694/