douji0108
douji0108
2015-02-28 10:40
浏览 33

从下拉列表中进行多项选择

Hie,I had asked this question already but I have make it briefly now please share some logic. 1st dropdown List-enter image description here Contain Class-1, class-2, class-3..etc. 2nd dropdown List-enter image description here Depends on class.If we chose class-1 and class-2 then respective Student Id will show on 2nd dropdown list. I want selected Classes in respective variables and selected turbines in respective variable. Then I will get this selected variables and use in another page.

I want multiple selection in each dropdown list. suppose I have select one Class-1 then, On the classId second dropdown list will fill with respective studentID,again I am select another class-2 then again Add respective studentID list in 2nd dropdown list.

In my databse script ClassId and StudentId this attribute present in same table only (not diffrent tables).

Table view somewhat like this->

 ClassID    StudentID   StudentNm 
      1        101         abc 
      1        102         xyz 
      1        103         jkl 
      2        201         uio
      2        202         tyu 
      3        302         qwe 
      3        305         zxc
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanputian5341
    duanputian5341 2015-02-28 11:30
    已采纳

    Try this

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <select multiple="multiple" name="classId" class="select-box">
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
        <option value="3">Option 3</option>
        <option value="4">Option 4</option>
        <option value="5">Option 5</option>
    </select>
    
     <script src="https://code.jquery.com/jquery-2.1.3.js"></script>
     <script>
        $('.select-box').change(function(){
            var classId = $(this).val();
    
            $.ajax({
                url : 'getSub.php',
                type: 'POST',
                dataType : 'JSON',
                data : {
                    'classId' : classId,
                },
                success : function(data){
                    console.log(data);
                }
    
            });
        });
     </script>
    </body>
    </html>
    

    When you print selectedValues values will be printed in array format. in getSub.php you can get values and send it as json

    getSub.php

    <?php
    
    $classIds = implode(',', $_POST['classId']);
    
    $stmet = "SELECT StudentID from TBL where ClassID IN ('$classIds')";
    
    $query = mysql_query($stmet);
    
    $result = mysql_fetch_array($query);
    
    echo json_encode($result);
    ?>
    
    点赞 评论
  • doucong8553
    doucong8553 2015-02-28 11:43

    try this sample

    your table shoule be like this

       CREATE TABLE 'data'
       (
       'id' int primary key auto_increment,
        'data' varchar(50),
         'weight' int(2),
       );
    
      CREATE TABLE `data_parent` 
       (
      `pid` int(11) primary key auto_increment,
      `did` int(11) unique,
      `parent` int(11),
      Foreign key(did) references data(id)
      )
    

    <script type="text/javascript" src="http://ajax.googleapis.com/
    ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function()
    {
    $(".country").change(function()
    {
    var id=$(this).val();
    var dataString = 'id='+ id;
    
    $.ajax
    ({
    type: "POST",
    url: "ajax_city.php",
    data: dataString,
    cache: false,
    success: function(html)
    {
    $(".city").html(html);
    } 
    });
    
    });
    
    });
    </script>
    Country :
    <select name="country" class="country">
    <option selected="selected">--Select Country--</option>
    <?php
    include('db.php');
    $sql=mysql_query("select id,data from data where weight='1'");
    while($row=mysql_fetch_array($sql))
    {
    $id=$row['id'];
    $data=$row['data'];
    echo '<option value="'.$id.'">'.$data.'</option>';
    } ?>
    </select> <br/><br/>
    
    City :
    <select name="city" class="city">
    <option selected="selected">--Select City--</option>
    </select>

    </div>
    
    点赞 评论

相关推荐