2016-07-28 73 views
0

这是我的HTML代码使用列表项是从数据库中foreach循环:在下拉列表中,onchange功能仅在第一项更改时有效?

<select class="form-control select" id="inventoryitem" name="inventoryitem" onchange="getunit();"> 
    <option>---select item---</option> 
    <?php foreach($item as $i) { ?> 
    <option><?php echo $i->Name; ?></option> 
    <?php } ?> 
</select> 

这是我的脚本代码:

function getunit() 
{ 
    var item = $('#inventoryitem').val(); 
    alert(item); 
} 
+0

你究竟想实现什么? –

+0

请给出更具体的细节,以了解您的实际问题。 – Vir

+0

只有当第一项变化意味着你想要得到的东西 – mean

回答

0

请删除的onchange = “getunit();”从您的选择 并尝试下面的代码:

$("#inventoryitem").change(function() { 
    var item = $("#inventoryitem option:selected").text(); 
    alert(item); 
}); 
0

JavaScript就停止功能,如果其中任何一个有错误,请 确保所有JavaScript是运作良好。

$(function(){ 
    $("#inventoryitem").change(function(){ 
    console.log($(this).val); // Instead of alert i prefer you to use console 
    }) 
}) 
相关问题