使用javascript将数据从数据库中提取到下拉列表中

时间:2019-07-04 07:34:54

标签: javascript node.js sqlite dropdown

在我的项目中,当您在文本框中输入数字时,会在文本框中显示动态下拉列表。默认情况下,只有一个下拉菜单包含来自数据库的数据。但是动态显示的下拉列表不包含数据,显示的是静态数据,并且没有将其保存在数据库中。

默认情况下,我在.ejs文件中编写了下拉菜单的代码,对于动态下拉菜单,我创建了一个script.js。

<div class="form-group row">                        
                    <div class="col-sm-6 col-lg-4">
                        <label class="control-label pt10">MEG Injection 
Location</label>
                    </div>                          
                    <div class="col-sm-6 col-lg-8">
                        <select id="inputState" class="form-control" name = "megInjectionLocation1">
                            <%  if(chkVal(dropDown.megInjectionLocation1)!=''){
                                            let selected = chkVal(data.megInjectionLocation1);
                                            let list = JSON.parse(dropDown.megInjectionLocation1);
                                            for (let key in list) {
                                    %>  
                                        <option <% if(selected !='' && selected == list[key]){ %> selected <% } %> value = "<%= list[key] %>"><%= list[key] %></option> 

                                    <%  }} %>
                        </select>
                    </div>
                </div>
                <% if(typeof(data.noMEG)!== 'undefined' && parseInt(data.noMEG) > 1){   %>
                    <div class = "megl-block">
                        <% for(let a=2; a <= parseInt(data.noMEG); a++){  %>
                            <div class="form-group row">                        
                                <div class="col-sm-6 col-lg-4">
                                    <label class="control-label pt10">MEG Injection Location<%= a %></label>
                                </div>                          
                                <div class="col-sm-6 col-lg-8">
                                    <select id="inputState" class="form-control" name = "megInjectionLocation<%= a %>">
                                        <%  if(chkVal(dropDown.megInjectionLocation)!=''){
                                            let selected = chkVal(data.megInjectionLocation);
                                            let list = JSON.parse(dropDown.megInjectionLocation);
                                            for (let key in list) {
                                    %>  
                                        <option <% if(selected !='' && selected == list[key]){ %> selected <% } %> value = "<%= list[key] %>"><%= list[key] %></option>
                                    <%  }} %>
                                    </select>
                                </div>
                            </div>
                        <% } %>
                    </div>  
                <%  }else{ %>
                    <div class = "megl-block" style = "display:none;"> </div>   
                <%  } %>



    function xtFormsLocation(elem){
    let field = $(elem).attr("name");
    let count = parseInt($(elem).val()); 
    var  htm = '';
    if(field == 'noMEG'){
        var flabel = 'MEG';
        var fname = 'megInjectionLocation';
        var fblock = '.megl-block';
    }else if(field == 'noMeOH'){
        var flabel = 'MeoH';
        var fname = 'meohInjectionLocation';
        var fblock = '.meohl-block';
    }else if(field == 'noSI'){
        var flabel = 'SI';
        var fname = 'siInjectionLocation';
        var fblock = '.sil-block';
    }else if(field == 'noCI'){
        var flabel = 'CI';
        var fname = 'ciInjectionLocation1';
        var fblock = '.cil-block';
    }else if(field == 'noAI'){
        var flabel = 'AI';
        var fname = 'aiInjectionLocation';
        var fblock = '.ail-block';
    }else if(field == 'noWI'){
        var flabel = 'WI';
        var fname = 'wiInjectionLocation';
        var fblock = '.wil-block';
    }
    if(count > 1 && count <= 4){
        for(let a=2; a <= count; a++){
            htm += `<div class="form-group row">                        
                        <div class="col-sm-6 col-lg-4">
                            <label class="control-label pt10">${flabel} 
Injection Location${a}</label>
                        </div>                          
                        <div class="col-sm-6 col-lg-8">
                            <select id="inputState" class="form-control" 
name = "${fname}${a}">
                                <option value = "@PMV-PWV">@PMV- 
PWV</option>
                            </select>
                        </div>
                    </div>`;
        }
        $(fblock).html(htm).show();
    }else {
        $(fblock).html('').hide();
    }
 }

它没有显示任何错误,但是下拉列表没有显示数据。 请帮我怎么做

0 个答案:

没有答案