如何使用jQuery从多个组合框中获取值

时间:2018-02-02 02:45:51

标签: jquery combobox

我有多个不同名称的组合框。请参阅以下代码:

for($i= 1 ; $i <= 5 ; $i++)
{
?>
    <select class="department" name="department[]" id="<?php echo $i; ?>">
        <option value="">Choose Department & HOD</option>

        <?php
        $qHOD = oci_parse($c1, "SELECT H.HOD_ID, H.HOD, H.DEPARTMENTID_FK, D.DEPARTMENTID, D.DEPARTMENTNAME, D.LOCATIONID_FK FROM WA_GA_TBL_HOD H, WA_GA_TBL_DEPARTMENTS D WHERE H.DEPARTMENTID_FK = D.DEPARTMENTID AND D.LOCATIONID_FK = 'LC0001' ORDER BY D.DEPARTMENTNAME ASC");
        oci_execute($qHOD);
        while($dHOD = oci_fetch_array($qHOD))
        {
        ?>
            <option value="<?php echo $dHOD['HOD_ID']; ?>"><?php echo $dHOD['DEPARTMENTNAME']; ?> - <?php echo $dHOD['HOD']; ?></option>
        <?php
        }
        ?>
    </select>
<?php
}
?>

现在我想获得组合框的值。

我用这个:

$(".department").bind("change", function(e)
{
    var department = $(".department").val(); 
    alert(department);
});

但我没有任何价值。

如何做到这一点?

1 个答案:

答案 0 :(得分:0)

我有解决方案。

在jQuery上我添加了这个函数

var department = [];
$('.department option:selected').each(function () {
    var val = $(this).val();
    if (val) {
    department.push(val);
        }
});