dongta5621 2014-12-12 17:30
浏览 62

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

I have arrays of select box which will output multiple select box depending on how many select box i wanna show. The function when i select the option is that it search room details in my database using ajax. The search function works but only in the first select box not for the rest of my select boxes. this is 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>

this is my ajax

<script>    
    $(document).ready(function(){

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

        });
    });
 });
</script>

this is my ajaxpage

if(isset($_POST['search'])){
 functions right here are select querys into table(this is working)
}

I really appreciate your help.

  • 写回答

2条回答 默认 最新

  • drpmazn9021 2014-12-12 17:32
    关注

    Try using syntax for delegated event as you are dynamically changing HTML. Use the class attribute to define the change event and use this inside the function as you have many select boxes.

    $(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);
            }
    
        });
    });
    

    Also you have to use a class attribute in select html as IDs are supposed to be unique.

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

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化