php:具有搜索功能的数组中的多个选择框

php:具有搜索功能的数组中的多个选择框,php,arrays,ajax,Php,Arrays,Ajax,我有一个选择框数组,它会根据我想显示的选择框的数量输出多个选择框。当我选择该选项时,它的功能是使用ajax在我的数据库中搜索房间详细信息。搜索功能只在第一个选择框中起作用,而不在我的其他选择框中起作用。 这是mypage.php <select name = "room[]" id = "search"> <option value = "none">&larr;Room</option> <?php $find_room = D

我有一个选择框数组,它会根据我想显示的选择框的数量输出多个选择框。当我选择该选项时,它的功能是使用ajax在我的数据库中搜索房间详细信息。搜索功能只在第一个选择框中起作用,而不在我的其他选择框中起作用。 这是mypage.php

<select name = "room[]" id = "search">
  <option value = "none">&larr;Room</option>
  <?php
    $find_room = DB::getInstance()->query("SELECT * FROM tbl_room WHERE room_status =  'ENABLED'");
if($find_room->count()){
   foreach($find_room->results() as $find_room){

    ?>
     <option value = "<?php echo $find_room->room_id; ?>"><?php echo $find_room->room_number; ?>
     </option>
 <?php

    }
  } 
  ?>
</select>

非常感谢您的帮助。

在动态更改HTML时,请尝试对委派事件使用语法。使用class属性定义
change
事件,并在函数中使用
this
,因为您有许多选择框

$(document).on("change",".search",function(){
    ele = $(this);
    var search = ele.val();
    $.ajax({
        type:"POST",
        url:"programhead_ajaxroom.php",
        data:{search:search},
        success:function(res){
            ele.html(res);
        }

    });
});
您还必须在selecthtml中使用class属性,因为id应该是唯一的

<select name = "room[]" id = "search" class="search">

哦,我错过的是循环遍历每个元素数组

 $(document).on("change","select[name^=search]",function(){
        check_obj = document.getElementsByName("search[]");
        for (i=0; i<check_obj.length; i++)
        {
            if (check_obj[i].value == "none")
            {}
            else{
                var search= check_obj[i].value;
            }
        }
        $.ajax({
            type:"POST",
            url:"programhead_ajaxroom.php",
            data:{search:search},
            success:function(res){
                $("#subjects").html(res);
            }

            });

    });
$(文档)。在(“更改”、“选择[name^=search]”上,函数(){
检查_obj=document.getElementsByName(“搜索[]”);

对于(i=0;i选择框的其余部分现在正在响应,但它正在返回第一个选择框的值。@StiEdu,所有选择框是否都具有相同的id“serch”?是的,先生。因为它在数组中,名称为“room[]”,id为“search”。假设我想输出多个选择框,具体取决于我想显示的内容。示例im仅显示3个选择框,内存分配后面的名称将是房间[0],房间[1],房间[2]。我是否可以通过名称而不是id来获取选择框的值?是的,您可以。检查此问题:。id也应该是唯一的。您也可以使用类似于房间[x]的内容来获取id的值。。。
<select name = "room[]" id = "search" class="search">
 $(document).on("change","select[name^=search]",function(){
        check_obj = document.getElementsByName("search[]");
        for (i=0; i<check_obj.length; i++)
        {
            if (check_obj[i].value == "none")
            {}
            else{
                var search= check_obj[i].value;
            }
        }
        $.ajax({
            type:"POST",
            url:"programhead_ajaxroom.php",
            data:{search:search},
            success:function(res){
                $("#subjects").html(res);
            }

            });

    });