java - 如何在选择同一 div 中存在的下拉列表时在 div 末尾附加动态生成的 HTML 表格?

标签 java javascript jquery html

我遇到了以下问题:

我在 html 中有这个 div:

<div id="PieChart">
    <label>Title of the pie chart:</label>
    <input id="pieChartTitle" type="text">
    <select id="xAxisList" onChange="showAxis('x')">
        <option hidden="true"></option>                         
        <option value="time">Time</option>
    </select>
</div>

下拉列表的OnChange 我正在调用一个Javascript 函数showAxis('x'),它会动态生成HTML 表。代码如下:

function showAxis(coordinate){
    var axisList = coordinate + "AxisList";
    var v = document.getElementById(axisList);
    selectedAxisType = v.options[v.selectedIndex].value;

    var dataArray = new Array();
    selectedChartType = "PieChart";    
    var selectedChart = document.getElementById(selectedChartType);

    var tr, td;
    var table, tableBody;
    var dropdown, opt, i;

    table = document.createElement('TABLE');

    dataArray = ["Years", "Seasons", "Months", "Weeks", "Days"];

    table.setAttribute("id", "time"+axisList+"Table");
    table.setAttribute("class", "time"+axisList+"Table");
    table.width='100%';

    tableBody = document.createElement('TBODY');
    table.appendChild(tableBody);

    dropdown = document.createElement("select");
    dropdown.setAttribute("id", "time"+axisList);
    tr = document.createElement('TR');
    tableBody.appendChild(tr);

    td = document.createElement('TD');
    td.width='40%';
    td.appendChild(document.createTextNode("Time selection on the basis of:"));
    tr.appendChild(td);

    opt = document.createElement("option");
    opt.hidden="true";
    dropdown.options.add(opt);

    for (i=0; i<dataArray.length; i++){
        td = document.createElement('TD');
        opt = document.createElement("option");
        opt.text = dataArray[i];
        opt.value = dataArray[i];
        dropdown.options.add(opt);
        td.appendChild(dropdown);

        tr.appendChild(td);
    }// END of Outer for loop
selectedChart.appendChild(table);
}// END of showAxis()

问题:showAxis(坐标)方法中动态生成的表被附加在“xAxisList”下拉列表之前,我希望将此表作为一个整体附加在下拉列表(“xAxisList”)之后。

我在几个博客上花了很多时间,但找不到任何有用的东西。如果有人帮助我解决这个问题,我将非常感激。感谢您抽出时间阅读我的问题。

最佳答案

检查这个 JS FIDDLE v1.9.1 在 javascript 而不是 html 中使用 onchange。

JSFIDDLE v1

(function($){

$(function(){  //document.ready


    $("#xAxisList").on('change', function() {
       showAxis('x'); 
    });

function showAxis(coordinate){
var axisList = coordinate + "AxisList";
var v = document.getElementById(axisList);
selectedAxisType = v.options[v.selectedIndex].value;

var dataArray = new Array();
selectedChartType = "PieChart";    
var selectedChart = document.getElementById(selectedChartType);

var tr, td;
var table, tableBody;
var dropdown, opt, i;

table = document.createElement('TABLE');

dataArray = ["Years", "Seasons", "Months", "Weeks", "Days"];

table.setAttribute("id", "time"+axisList+"Table");
table.setAttribute("class", "time"+axisList+"Table");
table.width='100%';

tableBody = document.createElement('TBODY');
table.appendChild(tableBody);

dropdown = document.createElement("select");
dropdown.setAttribute("id", "time"+axisList);
tr = document.createElement('TR');
tableBody.appendChild(tr);

td = document.createElement('TD');
td.width='40%';
td.appendChild(document.createTextNode("Time selection on the basis of:"));
tr.appendChild(td);

opt = document.createElement("option");
opt.hidden="true";
dropdown.options.add(opt);

for (i=0; i<dataArray.length; i++){
    td = document.createElement('TD');
    opt = document.createElement("option");
    opt.text = dataArray[i];
    opt.value = dataArray[i];
    dropdown.options.add(opt);
    td.appendChild(dropdown);

    tr.appendChild(td);
}// END of Outer for loop
selectedChart.appendChild(table);
}// END of showAxis

      });

})(jQuery);   

关于java - 如何在选择同一 div 中存在的下拉列表时在 div 末尾附加动态生成的 HTML 表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29997227/

相关文章:

java - 您如何使用 Java 正则表达式捕获和重用匹配项?

javascript - 通过javascript中的对象数组进行映射

javascript - 在 node.js 中检索经过 Twitter 验证的用户的推文

jquery - Html/Css 在一个 div 中垂直居中一个 div

javascript - 在某些窗口宽度处偏移 1 个像素

java - Hibernate - 如何在不加载到内存的情况下删除条件?

java - 模式优化

jQuery 警报框

java - 分割字符串时的"java.lang.NullPointerException"

javascript - 为 DOM 操作创建自定义脚本