要实现的功能是,点击select输入框,数据库里面的数据会以option弹出。

这需要用到ajax异步连接数据库

下面贴出代码

先说明一下后台传递的数据是json,以map的形式传入的。后台代码很简单,这里就不累赘了

直接贴出前台代码了

 <select >
        </select>

下面是js代码

    function  find(){
            if(document.getElementById("courseSelect").length==0){
                $.ajax({
                    type:"get",
                    async:true,
                    url:"curriculum.do",
                    dataType:"json",   //必写
                    data: {method:"getAllCourseName"},
                    contentType: "application/x-www-form-urlencoded; charset=utf-8",  //防止提交中文乱码
                    success:function(msg){
                        for(var mi in msg){        //循环json
                            var txt2=$("<option value=msg[mi]></option>").text(msg[mi].name);
                            $("select").append(txt2); 
                        }
                    }  
                    });   
                    return true;
                    }else{
                    return false;
                    }
        }

select中用的是onclick,js语句中记得要用if判断一下

相关文章:

  • 2022-12-23
  • 2022-01-15
  • 2022-12-23
  • 2022-12-23
  • 2021-05-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-20
  • 2022-12-23
  • 2021-05-19
  • 2022-12-23
  • 2021-12-03
相关资源
相似解决方案