简体   繁体   English

当调用JS函数时,从数据库填充下拉菜单的值?

[英]Populate a dropdown menu's values from a database when a JS function is called?

Similar to this fiddle: http://jsfiddle.net/vigneshmoha/bbxMe/2/ 与此小提琴类似: http : //jsfiddle.net/vigneshmoha/bbxMe/2/

<html>
<head>
<script>
function dateGenerate() {
       var date = new Date(), dateArray = new Array(), i;
       curYear = date.getFullYear();
        for(i = 0; i<5; i++) {
            dateArray[i] = curYear+i;
        }
        return dateArray;
}
// The Above function will create an array of five consecutive year from the the current year.

function addSelect(divname) {
    var newDiv=document.createElement('div');
    var html = '<select>', dates = dateGenerate(), i;
    for(i = 0; i < dates.length; i++) {
        html += "<option value='"+dates[i]+"'>"+dates[i]+"</option>";
    }
    html += '</select>';
    newDiv.innerHTML= html;
    document.getElementById(divname).appendChild(newDiv);
}

</script>

</head>

<body>

<div id="select-container">
</div>
<button id="add" onclick="addSelect('select-container');">Add Dropdown</button>

</body>
</html>

Except I have a MySql database with records that I want to populate the dropdown list with (First and Last names in this case). 除了我的MySql数据库外,我想用(在这种情况下为名字和姓氏)填充下拉列表的记录。 Does anyone have a resource or example on what could achieve this? 有没有人有什么资源或例子可以实现这一目标? I was told AJAX might be able to help here, but I am somewhat unfamiliar with the language. 有人告诉我AJAX可以在这里提供帮助,但是我对这种语言不熟悉。 Any help would be appreciated. 任何帮助,将不胜感激。

var e = document.getElementById("YourDrodownboxVale");
var _id = e.options[e.selectedIndex].value;

/*This is to post some data from database */ / *这是从数据库中发布一些数据* /

$.ajax({
            url: uploadURI,
            type: 'post',
            data: {id: _id},
            success: function () {
                alert('Susccess');
            }
        });

/*This is to get some data from database */ / *这是从数据库中获取一些数据* /

    $.ajax({
            url: uploadURI,
            type: 'get',
            data: {id:_id},
            success: function (data) {
                //put the resulting data hear
                document.getElementById("YourDivID").html = data;
            }
        });

//This is another method. //这是另一种方法。 Functions to open database and to create, insert data into tables 用于打开数据库以及创建数据并将其插入表的功能

getSelectedRow = function(val)
{
    db.transaction(function(transaction) {
          transaction.executeSql('SELECT * FROM Employ where number = ' + parseInt(val, 10) + ';',[], selectedRowValues, errorHandler);

    });
};
selectedRowValues = function(transaction,results)
{
     for(var i = 0; i < results.rows.length; i++)
     {
         var row = results.rows.item(i);
         alert(row['number']);
         alert(row['name']);                 
     }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM