强制onchange到特定值

时间:2015-09-09 12:26:15

标签: javascript php html

我有一个HTML代码和Javascript看起来像这样:

<td>Item Category</td>
                <td>
                    <select id="item_category_selection" onchange="item_category(value)">
                        <option>Select</option>
                        <?php
                            $type_result = mysql_query("SELECT DISTINCT ItemCategory FROM ".$dmt_objects." ORDER BY ItemCategory ASC ");
                            while($type_array = mysql_fetch_array($type_result)){
                                if($type_array['ItemCategory'] !== "N/A" && $type_array['ItemCategory'] !== "Item"){
                                echo "<option>".$type_array['ItemCategory']."</option>";
                            }
                        }

                        ?>
                    </select>
                </td>

这会产生一个包含不同选项的列表,但是它们没有任何值。它看起来像这样:

<select id="item_category_selection" onchange="item_category(value)">
                        <option>Select</option>
                        <option>Alt 1</option>
                        <option>Alt 2</option>

调用的函数如下所示:

function item_category(val){
        selected_item_category = $("#item_category_selection").val();

        document.getElementById("sel_target").innerHTML = "";
        $.ajax({
            type: "POST",
            url: "find_objects.php",
            data: {selected_item_category:selected_item_category, selected_project:selected_project},
            dataType: 'json',
            success:  function(data){
                $('#sel_target').append("<option value='Select value' >Select Value</option>");
                $('#sel_target').append("<option value='PAINT ALL' >Show All</option>");
                var array_length = data.length;

                for(var i = 0; i < array_length; i++){
                    var ItemName = data[i].ItemName;
                    var DatabaseID = data[i].DatabaseID;

                    $('#sel_target').append("<option value='"+DatabaseID+"' >" + ItemName + "</option>");



                }
                return false;
            }
        });
    }

现在,我想强制它为一个选项,并调用该函数。我怎么做?当您单击其他按钮时,我希望将Itemcategory更改为特定的按钮。

0 个答案:

没有答案