【问题标题】:How to display data from database with onclick in dropdown?如何在下拉菜单中使用 onclick 显示数据库中的数据?
【发布时间】:2017-04-29 03:56:47
【问题描述】:

我有一个“问题”表,其中最多有 10 个选项和 1 个答案类型,在 answer_type 列中声明。

首先,这是我根据文本框中的用户输入显示下拉菜单的代码。这些生成的下拉菜单包含问题

<script>
        $(document).ready(function(){
            $("#execute").click(function(){
                var numQ = +$('#q_num').val();
                //Loop--
                for(var ctr=0; ctr < numQ; ctr++){
                    var str = load_questions();
                    $("#divQuestions").append(str);
                }
            });
        });
    </script>

输入问题数量后,需要选择一个类别或子类别才能实际生成下拉列表的数量,因为所有问题都在一个类别或子类别下。

function load_questions(){

    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","ajax.php??main=1&subcategory="+document.getElementById("subcategorydd").value +"&cnt="+document.getElementById("q_num").value,false);
    xmlhttp.send(null);
    document.getElementById("question").innerHTML=xmlhttp.responseText;


}

当我在下拉列表中选择一个问题并显示它(无论 answer_type 是什么,例如复选框、单选按钮)时,我如何做一个 onclick 触发器也将显示答案类型?

【问题讨论】:

    标签: javascript php jquery mysql ajax


    【解决方案1】:
    <script> 
    $('#group').change(function (){  
    $.ajax({
                type: "GET",
                url: "ajax.php??main=1&subcategory",     
                data: {main:'1', subcategory:$('#subcategorydd').val(), cnt:$('#q_num').val()},        
                dataType: "html",   //expect html to be returned    
                async:false,            
                success: function(response){
    
                    $('#divQuestions').append= response; 
    get_table();
    
                }
            }); 
    }); 
    </script>
    

    从php端

    foreach($group->result() as $grp){
                echo '<option value="'.$grp->SubNum.'">'.$grp->SubCategory.'</option>';
            }
    

    或者您可以提交自己的查询样式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2011-11-12
      • 1970-01-01
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多