jQuery & Ajax combobox - 不同的选择标签名称


jQuery & Ajax combobox - different select tag names

我正在练习这个

这是一个工作演示

在这里我可以重复代码:

j查询代码:

        $(document).ready(function() {
        //$('#loader').hide();
        $('.parent').livequery('change', function() {
            $(this).nextAll('.parent').remove();
            $(this).nextAll('label').remove();
            $('#show_sub_categories').append('<img src="loader.gif" style="float:left; margin-top:7px;" id="loader" alt="" />');
            $.post("get_chid_categories.php", {
                parent_id: $(this).val(),
            }, function(response){
                setTimeout("finishAjax('show_sub_categories', '"+escape(response)+"')", 400);
            });
            return false;
        });
    });
    function finishAjax(id, response){
      $('#loader').remove();
      $('#'+id).append(unescape(response));
    }

.HTML:

<div id="show_sub_categories">
    <select name="search_category" class="parent">
    <option value="" selected="selected">-- Categories --</option>
    <?php
    $query = "select * from ajax_categories where pid = 0";
    $results = mysql_query($query);
    while ($rows = mysql_fetch_assoc(@$results))
    {?>
        <option value="<?php echo $rows['id'];?>"><?php echo $rows['category'];?></option>
    <?php
    }?>
    </select>   
</div>

get_child_categories.php

<?php
include('dbcon.php');
if($_REQUEST){
$id = $_REQUEST['parent_id'];
$query = "select * from ajax_categories where pid = ".$id;
$results = @mysql_query( $query);
$num_rows = @mysql_num_rows($results);
if($num_rows > 0)
{?>
    <select name="sub_category" class="parent">
    <option value="" selected="selected">-- Sub Category --</option>
    <?php
    while ($rows = mysql_fetch_assoc(@$results))
    {?>
        <option value="<?php echo $rows['id'];?>"><?php echo $rows['category'];?></option>
    <?php
    }?>
    </select>   
<?php   
}
else{echo '<label style="padding:7px;float:left; font-size:12px;">No Record Found !</label>';}
}
?>

它是使用jQuery,ajax和php的下拉列表(组合框)的N级动态加载,从mySql数据库中获取项目。 一切都很好,但我的 HTML 中有这个:

<select name="search_category" class="parent">...</select>  
<select name="sub_category" class="parent">...</select>  
<select name="sub_category" class="parent">...</select>  
<select name="sub_category" class="parent">...</select>  

我想为每个选择标签选择项目,但由于它们具有相同的名称属性值,我认为我做不到。 请告诉我一些将名称更改为sub_category1、sub_category2、...或其他一些工作方法。

这是我

找到的解决方案:

我在显示其内容的最后一个组合之后添加了此脚本:

$("#show_sub_categories select").attr("id", function(i) {
    return "combo_" + i;
});

把它放在最后,否则get_child_categories.php
干杯!