JQuery选择:不在后台更新选择选项

时间:2016-11-23 14:08:23

标签: javascript php jquery ajax jquery-chosen

我正在使用JQuery Chosen插件来理解我的选择选项。插件本身工作正常。但是我的一些下拉菜单使用AJAX POST来过滤/撤回另一个Select中的精选选项列表。

JQuery插件已添加但未更改。但由于某种原因,当我在选择过滤次要选择时选择一个选项时,辅助选择似乎不会识别出已选择了一个选项。

有什么想法吗?有没有人遇到过这个问题?

            <div class="search-line">
                <div class="search-option">
                    <label>Asset Type:</label>
                    <select name="AssetType" id="AssetType">
                        <?php

                        $type_sql = "SELECT DISTINCT AssetType.AssetTypeTitle AS HardwareAssetAssetTypeTitle, HardwareAssetAssetTypeID FROM HardwareAsset INNER JOIN AssetType ON (AssetType.AssetTypeID = HardwareAsset.HardwareAssetAssetTypeID) ORDER BY HardwareAssetAssetTypeTitle ASC";

                        $type = sqlsrv_query($database_connection, $type_sql);

                        if (!sqlsrv_has_rows($type)){
                            echo "<option>No Records Found</option>";
                        }
                        else{
                            echo "<option value= ''>Select Asset Type</option>";
                            while($type_option = sqlsrv_fetch_object($type)){
                                echo "<option value='$type_option->HardwareAssetAssetTypeID'>".$type_option->HardwareAssetAssetTypeTitle."</option>";
                            }
                        }
                        ?>
                    </select>
                </div>
                <div class="search-option">
                    <label>Asset Sub-Type:</label>  
                    <select name="AssetSubType" id="AssetSubType">
                        <option value="">Select Asset Type First</option>
                    </select>
                </div>
            </div>

AJAX:

$('#AssetType').on('change',function(){
    var AssetAssetTypeID = $(this).val();
    if(AssetAssetTypeID != 0){
        $.ajax({
            type:'POST',
            url:'/ITSMIS/data/asset/search.php',
            data:'AssetAssetTypeID='+AssetAssetTypeID,
            success:function(data){
                $('#AssetSubType').html(data);
            }
        }); 
    }
    else{
        $('#AssetSubType').html('<option value="">Select Asset Type First</option>');
    }
});

1 个答案:

答案 0 :(得分:1)

我以前有这个,不知道为什么但是像下面这样的onchange对我有用:

$('body').on('change','#AssetType',function(){
    var AssetAssetTypeID = $(this).val();
    if(AssetAssetTypeID != 0){
        $.ajax({
            type:'POST',
            url:'/ITSMIS/data/asset/search.php',
            data:'AssetAssetTypeID='+AssetAssetTypeID,
            success:function(data){
                $('#AssetSubType').html(data);
            }
        }); 
    }
    else{
        $('#AssetSubType').html('<option value="">Select Asset Type First</option>');
    }
});