How to use a jQuery autocomplete combobox with AJAX JSON data?

前端 未结 2 1603
日久生厌
日久生厌 2021-01-12 10:56

I need to do the following using a combobox.

  • Select box has a default list of cities which the user can search from.
  • If a user types in t
2条回答
  •  没有蜡笔的小新
    2021-01-12 11:35

    Why don't you duplicate the plugins and two combo boxes.

    Then:

         $( "#combobox1" ).combobox1({
              select: function (event, ui) { 
               var value = ui.item.value;/*Whatever you have chosen of this combo box*/
                $.ajax({
                  type: "POST",
                  dataType: 'json',
                  url: "script.php",
                  data: {
                    'anything':value
                  },
                  success: function(res)
                  {
                    /*replace your next combo with new one*/
                    $("select#combobox2").html(res);
                  }
              });
            }
        });
       $( "#toggle" ).click(function() {
        $( "#combobox1" ).toggle();
       });
    
       $( "#combobox2" ).combobox2();
    
       $( "#toggle" ).click(function() {
        $( "#combobox2" ).toggle();
       });
    

    PHP file (This is based on Codeigniter):

    db->query("SELECT * FROM table WHERE field='".$keyword."'");
       $data.= "";
       if($query4->num_rows() > 0)
       {
           foreach($query5->result_array() as $row)
           {
             $data.= "";
           }
       }
       echo json_encode($data);
    }
    ?>
    

    Hope this helps.

提交回复
热议问题