Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

javascript - I am new to node js how to get dependent dynamic dropdown from database

my node code:

app.get('/block_name', function (req,res){

var sql='SELECT  `block_name`,`block_id` FROM `tbl_block`  ';
connection.query(sql,function(err, result) {
    if (err) throw err;
    res.json(result);
});

});

app.get('/site_name', function (req,res){

var sql='SELECT  `site_name`,`site_id` FROM `tbl_site` ';
connection.query(sql,function(err, result) {
    if (err) throw err;
    res.json(result);
});

});

HTML

Site

      <select name="site" id="dropdown" >
        <option value="">- select -</option>
      </select>
     <label class="field" >Block</label>
      <select name="block" id="dropdown1" >
        <option value="">- select -</option>
      </select>

SCRIPT

<script>

    $(document).ready(function(){
    $.ajax({
        url : "/site_name",
        type : "GET",
        success : function(data){

            var len = data.length;

            console.log(data);
            $("#dropdown").empty();
            for(var i =0; i<len;i++){
            var value1 = data[i]['site_name'];
            var value2 = data[i]['site_id'];
                $("#dropdown").append("<option value='"+value2+"' >"+value1+"</option>");

            }

        }
    });
}); 

    $(document).ready(function(){
    $.ajax({
        url : "/block_name",
        type : "GET",
        success : function(data){

            var len = data.length;

            console.log(data);
            $("#dropdown1").empty();
            for(var i =0; i<len;i++){
            var value1 = data[i]['block_name'];
            var value2 = data[i]['block_id'];
                $("#dropdown1").append("<option value='"+value2+"' >"+value1+"</option>");

            }

        }
    });
}); 


 </script>

my second dropdown lists all the data but i need particular thing to be listed according to the first dropdown. Thanks in advance.. Sorry for my bad English

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Anytime the first drop-down changes value, you need to repopulate the second drop-down based on the select value in the first drop-down - usually by rerunning your ajax call to load the second drop-down, but using the value of the first drop-down to decide which data to request from the server.

You would do this by installing an event handler on the first drop-down so you can detect any time its value is changed and then you would repopulate the second dropdown with a fresh ajax call whenever that event on the first drop-down occurs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...