处理下拉菜单
我的 html 中有一个下拉菜单,我想要的是当菜单更改时,调用 JS 函数发送所选值。我现在所拥有的是:
HTML/PHP:
<select name="selectSquad" class="SquadWeaponSelector" id="selectSquad" onchange="javascript:showWeaponEditorWindow(this.form.selectSquad);">
<?PHP
$max = $squadNumbers - 1;
$i = 0;
while($i <= $max){
echo "<option value=\"".$names_split[$i]."\"/>".$names_split[$i]."</option>";
$i++;
}
?>
</select>
JavaScript - 我想要发生的事情:
function showWeaponEditorWindow(squad){
if(squad == "A PHP Value - Jack"){
alert("jack selected");
}
}
我将如何实现这一目标?
I have a drop down menu in my html, and what I want is when the menu is changed, a JS function is called sending the selected value. What I have right now is this:
HTML/PHP:
<select name="selectSquad" class="SquadWeaponSelector" id="selectSquad" onchange="javascript:showWeaponEditorWindow(this.form.selectSquad);">
<?PHP
$max = $squadNumbers - 1;
$i = 0;
while($i <= $max){
echo "<option value=\"".$names_split[$i]."\"/>".$names_split[$i]."</option>";
$i++;
}
?>
</select>
JavaScript - what I want to happen:
function showWeaponEditorWindow(squad){
if(squad == "A PHP Value - Jack"){
alert("jack selected");
}
}
How would I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个,
您需要在您的条件中分配
squad.value
。更新:
您可以使用
selectedIndex
获取选定的菜单,记住菜单索引从 0 开始;因此将
squad.value
更改为try this,
you need to assign
squad.value
in your condition.Update:
you could use
selectedIndex
to get the selected menu rember the the menu index starts at 0;so change
squad.value
to我建议您使用 jQuery 来实现此目的。你可以写:
I recommend that you use jQuery for this purpose. You can write: