Javascript 在特定区域显示我的下拉框

Javascript 在特定区域显示我的下拉框,javascript,jquery,Javascript,Jquery,好的,这是我正在做的。基于某个下拉列表值,我正在设置另一个下拉列表值。 是否可以根据我现有的代码,在某个特定区域显示下拉列表 if (SelectedIndex == 2 || SelectedIndex == 5 || SelectedIndex == 7) { $("#DomContainer").remove(); var MainContainer = document.createElement("Div");

好的,这是我正在做的。基于某个下拉列表值,我正在设置另一个下拉列表值。 是否可以根据我现有的代码,在某个特定区域显示下拉列表

        if (SelectedIndex == 2 || SelectedIndex == 5 || SelectedIndex == 7) {
            $("#DomContainer").remove();
            var MainContainer = document.createElement("Div");
            MainContainer.id = "DomContainer";
            $("body").append(MainContainer);                
            var Options = new Array();
            for(var i=1;i<=28;i++){
               Options.push(i);
            }
            AddDropDown(Options);
        }

    function AddDropDown(Options) {
        var selectHTML = "<label>Day:</label>&nbsp;";
        selectHTML += "<select>";
        for (i = 0; i < Options.length; i = i + 1) {
            selectHTML += "<option value='" + Options[i] + "'>" + Options[i] + "</option>";
        }
        selectHTML += "</select>";
        document.getElementById("DomContainer").innerHTML = selectHTML;
    }

For example <div id="new_drop">//Display the drop down here </div>
if(SelectedIndex==2 | | SelectedIndex==5 | | SelectedIndex==7){
$(“#DomContainer”).remove();
var MainContainer=document.createElement(“Div”);
MainContainer.id=“DomContainer”;
$(“正文”).append(主容器);
var Options=新数组();

对于(var i=1;i只需在
AddDropDown
中添加第二个参数,用于传递要插入下拉列表的容器的ID:

function AddDropDown(Options, containerId) {
    ...
    document.getElementById(containerId).innerHTML = selectHTML;
那么就这样称呼它:

AddDropDown(Options, "new_drop");

(如果我理解正确)

只需在
AddDropDown
中添加第二个参数,用于传递要插入下拉列表的容器的ID:

function AddDropDown(Options, containerId) {
    ...
    document.getElementById(containerId).innerHTML = selectHTML;
那么就这样称呼它:

AddDropDown(Options, "new_drop");

(如果我理解正确的话)

无需从DOM中删除和附加元素,只需替换现有的元素内容……这里是一个简化版本

// if "SelectedIndex" is one of the values in the array provided
if ($.inArray(SelectedIndex, [2,5,7])) {
        $("#DomContainer").html("<label>Day:</label>&nbsp;"+inject_dropdown(28));
}    

// Creates and returns a new drop-down based on the "length" provided
function inject_dropdown(length){
    var selectHTML = "<select>";
    for( i=1; i<=length; i++ ){
        selectHTML += "<option value='" + i + "'>" + i + "</option>"
    }
    selectHTML += "</select>"
    return selectHTML;
}
//如果“SelectedIndex”是提供的数组中的值之一
if($.inArray(SelectedIndex[2,5,7])){
$(“#DomContainer”).html(“日:+inject_下拉列表(28));
}    
//根据提供的“长度”创建并返回新的下拉列表
函数注入_下拉列表(长度){
var selectHTML=“”;

对于(i=1;i无需从DOM中删除和附加元素,只需替换现有的元素内容……这里是一个简化版本

// if "SelectedIndex" is one of the values in the array provided
if ($.inArray(SelectedIndex, [2,5,7])) {
        $("#DomContainer").html("<label>Day:</label>&nbsp;"+inject_dropdown(28));
}    

// Creates and returns a new drop-down based on the "length" provided
function inject_dropdown(length){
    var selectHTML = "<select>";
    for( i=1; i<=length; i++ ){
        selectHTML += "<option value='" + i + "'>" + i + "</option>"
    }
    selectHTML += "</select>"
    return selectHTML;
}
//如果“SelectedIndex”是提供的数组中的值之一
if($.inArray(SelectedIndex[2,5,7])){
$(“#DomContainer”).html(“日:+inject_下拉列表(28));
}    
//根据提供的“长度”创建并返回新的下拉列表
函数注入_下拉列表(长度){
var selectHTML=“”;
对于(i=1;i