使用 JavaScript 创建动态下拉列表


create dynamic drop-downs using javascript

您好,我想使用 javascript 创建动态下拉列表,但它不起作用。

我的PHP代码:

<input type="button" onclick="javascript:addRow();" value="Add Row" name="addRow"/>

我的JS代码:

// This function is used to create dynamic form element
function addRow() {
    var opt = document.createElement("option");
    element = document.createElement("select");
    element.setAttribute('id', 'focus');
    element.options.add(opt);
}

PS:它没有给出任何js控制台错误。

您不会将元素添加到正文中,因此它不会显示

// This function is used to create dynamic form element
function addRow() {
    var opt = document.createElement("option");
    element = document.createElement("select");
    element.setAttribute('id', 'focus');
    element.options.add(opt);
    document.body.appendChild(element);
}