简体   繁体   English

我如何从一个 <li> 下拉菜单是php用JavaScript还是jquery生成了第二个li菜单?

[英]How can I generate from a <li> drop down menu that is php generated a second li menu with javascript or jquery?

I create a drop down menu with a foreach cycle querying the db . 我使用查询数据库的foreach周期创建一个下拉菜单。

Now i would like that when i choose a subitem from the menu via javascript get the value and with a ajax request take all the values that are in the sub-menu for the item choses in the first menu and returned to javascript or jquery that append in the html in a second li section separated. 现在,我想当我通过javascript从菜单中选择一个子项时,获取值并通过ajax请求获取第一个菜单中所选项目的子菜单中的所有值,并返回到javascript或附加的jquery在html中的第二个li部分中分隔。

the first menu was genereated by a table area, the second menu it is generated by a table room this is the structure: 第一个菜单由表区域生成,第二个菜单由表室生成,其结构如下:

area alpha  -->  room 1, room 2, room 3
area beta -->    room 4, room 5
area gamma  -->   romm6 room 7

...

The following code was not so successful! 以下代码不太成功!

THe main problem are: 主要问题是:

1) retrieving the data from li tag area 2) the ajax query that have to get the rooms, in an array maybe 3) append the rooms in a second li tag room in html for showing the rooms javascript and jquery code: 1)从li标签区域检索数据2)必须获取房间的ajax查询,可能在数组中3)将房间附加到html的第二个li标签空间中,以显示房间javascript和jquery代码:

function chooseroomajax(id_box, id, db){

         var content=$('#'+id_box).val();
         var requestaj = $.ajax({
              type: "POST",
              url: "ajaxsave.php?action=select_room",
              data: "id="+ id +"&db=" + db +"&content=" + content +"&field=" + id_box,
              success: function(msg){
                   //alert( "select a: "+id_box+" runs good" );
              }
         });

        requestaj.done(function(data) {
            alert(data);

        for (var i = 0; i <= data.lenght; i++){

                $("#room").after( ' </br><a class="avatar"><img id ="fotopaz" src= "http://185.17.106.207:9997/crm/foto/'+ data+'" alt="no foto" width="90" height="90" align="left" title="Controlla" /></a></br></br></br>');


        }   

        });



    }





function chooseroom() {  


  //This way We Can get data from the li tag between ul
   $("#reparto li").click(function() {
  //alert(this.id); // id of clicked li by directly accessing DOMElement property
  //alert($(this).attr('id')); // jQuery's .attr() method, same but more verbose
  //alert($(this).html()); // gets innerHTML of clicked li
  alert($(this).text()); // gets text contents of clicked li
    var area2 = $(this).attr('id');
    });
   chooseromeajax(area2, "", "crmappdb");

}

HTML PHP code: HTML PHP代码:

<li id ="liarea" class="dropdown" " >

   <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user"></i> Area<b   class="caret"></b></a>

   <ul id = "area" class="dropdown-menu">
    <?php

      foreach($db->query("SELECT * FROM area ") as $row) {
    ?>

       <li id = "<?php echo $row['idarea']; ?>" onclick ="chooseroom()" > 
        <?php echo $row['descrizione'];    
   ?>
       </li>
    <?php
      }
   ?>
   </ul>
</li>

    <li id = "room">

    </li>
</li>

ajaxsave.php: ajaxsave.php:

if ($_REQUEST['action']=='select_room'){

$db=$_REQUEST['db'];
$campo=$_REQUEST['field'];  
$contenuto=$_REQUEST['content'];
$id=$_REQUEST['id'];  

$db = new PDO('mysql:host=localhost;dbname=crmappdb;charset=utf8', 'root', '', array(PDO::ATTR_EMULATE_PREPARES => false, 
                                                                                                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));


foreach($db->query("SELECT * FROM area WHERE descr = $field") as $row) {
   $idarea = $row['idarea'];

}


foreach($db->query("SELECT * FROM room WHERE area = $idarea") as $row) {
   $idroom = $row['idroom'];

}   
if (isset($idstanza)){

}else{
    $idroom = "no rooms";
}

echo $field ;



}

What you're asking for is called "cascading dropdowns" and there are many open-source implementations for it. 您要的是所谓的“级联下拉菜单”,并且有许多开源实现。 One example is this jQuery plugin: 这个jQuery插件就是一个例子:

http://www.dnasir.com/github/jquery-cascading-dropdown/demo.html http://www.dnasir.com/github/jquery-cascading-dropdown/demo.html

I think it easier if you using SELECT tag since it can control it event by onChange. 我认为使用SELECT标记会更容易,因为它可以通过onChange控制事件。

for your case, just create 2 dropdown one with generated php list and another one that will be generated by js and just should leave it blank for now 对于您的情况,只需创建2个下拉列表,其中一个带有生成的php列表,另一个将由js生成,现在应该将其留空

<div class="dropdown">
    <button>...</button>
    <ul id="liarea">
    <!-- generated li here -->
    </ul>
</div>
<div class="dropdown">
    <button>...</button>
    <ul id="room"></ul> <!-- empty list -->
</div>

Then on js, just do click event using jquery. 然后在js上,只需使用jquery进行click事件即可。 in your code right now you are using inline javascript on each list then call another click event inside that function. 现在在您的代码中,您正在每个列表上使用嵌入式javascript,然后在该函数内调用另一个click事件。

$('#liarea').on('click', 'li', function(){
    // ajax call and append the #room list
});
$('#room').on('click', 'li', function(){
    // any function you want to
});

Check this jsfiddle 检查这个jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM