2011-11-14 71 views
0

我在我的html中有一个下拉菜单,我想要的是当菜单改变时,JS函数被称为发送选定的值。我现在所拥有的是这样的:处理DropDown菜单

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"); 
    } 
} 

我将如何实现这一目标?

回答

1

试试这个, 你需要在你的病情分配squad.value

<select name="selectSquad" class="SquadWeaponSelector" id="selectSquad" onchange="showWeaponEditorWindow(this);"> 


function showWeaponEditorWindow(squad){ 
    if(squad.value == "A PHP Value - Jack"){ 
    alert("jack selected"); 
    } 
} 

更新:

你可以使用selectedIndex获取所选菜单的rember的菜单指数从0开始;

所以更改squad.value

(squad.selectedIndex == 1) 
+0

很高兴看到有人不直接跳到jQuery的一个简单的JavaScript任务 –

+0

感谢大卫。如果我想使用价值的位置呢? EG:如果squad.position选择== 1 –

+0

原因是因为我的下拉菜单是动态的...所以没有办法知道那里会有什么 –

0

我建议您为此使用jQuery。你可以写:

$('#selectSquad').change(function(){ 
    var selectedValue = $(this).val(); 
    // Do anything here with the selectedValue variable 
});