jQuery 运行函数组合框填充多个组合框

jQuery running the function combobox populate for multiple combobox

本文关键字:组合 填充 运行 jQuery 函数      更新时间:2023-09-26

I have combobox

 <select name="search_category[]"  id="search_category_id">
    <option value="" selected="selected"></option>
    <?php
    $query = "SELECT * FROM t_category ORDER BY category_name ASC";
    $results = mysql_query($query);
    while ($rows = mysql_fetch_assoc(@$results))
    {?>
        <option value="<?php echo $rows['category_id'];?>"><?php echo $rows['category_name'];?></option>
    <?php
    }?>
</select>
<span id="show_sub_categories" align="center">

这是 JS

jQuery(document).ready(function($){
    $('#loader').hide();
    $('#show_heading').hide();
$('#search_category_id').change(function(){
    $('#show_sub_categories').fadeOut();
    $('#loader').show();
    $.post("get_chid_categories.php", {
        parent_id: $('#search_category_id').val(),
    }, function(response){
        setTimeout("finishAjax('show_sub_categories', '"+escape(response)+"')", 400);
    });
    return false;
});
function finishAjax(id, response){
 $('#loader').hide();
 $('#show_heading').show();
 $('#'+id).html(unescape(response));
 $('#'+id).fadeIn();
} 
function alert_id()
{
if($('#sub_category_id').val() == '')
alert('Please select a sub category.');
else
alert($('#sub_category_id').val());
return false;
}

如果我有多个具有不同 id 的组合框怎么办?

例:

<select name="search_category[]"  id="search_category_id1">
    <option value="" selected="selected"></option>
    <?php
    $query = "SELECT * FROM t_category ORDER BY category_name ASC";
    $results = mysql_query($query);
    while ($rows = mysql_fetch_assoc(@$results))
    {?>
        <option value="<?php echo $rows['category_id'];?>"><?php echo $rows['category_name'];?></option>
    <?php
    }?>
</select>
<select name="search_category[]"  id="search_category_id2">
    <option value="" selected="selected"></option>
    <?php
    $query = "SELECT * FROM t_category ORDER BY category_name ASC";
    $results = mysql_query($query);
    while ($rows = mysql_fetch_assoc(@$results))
    {?>
        <option value="<?php echo $rows['category_id'];?>"><?php echo $rows['category_name'];?></option>
    <?php
    }?>
</select>

我希望函数为那 2 个组合框运行。但是当尝试运行代码时,该函数仅针对第一个组合框运行。(该函数基于第一个组合框填充第二个组合框)。

您需要

$.post.success处理程序中加载第二个select。示例(未经测试):

$.post("get_chid_categories.php", 
    { parent_id: $('#search_category_id1').val() },
    function (response){
        loadSelect("search_category_id2", response);
    }
);
function loadSelect(id, response){
    $('#loader').hide();
    $('#show_heading').show();
    var target = $('#' + id);
    target.html(decodeURIComponent(response));
    if (!target.is(":visible")) {
        target.fadeIn();
    }
}