javascript - 动态下拉问题

标签 javascript html

我想创建一个动态下拉列表,我在此链接 Dynamic Drop Down 上关注

但是根据我的结果,我得到了这段代码,但有些问题??? 什么也没发生,它保持空白,控制台中没有错误或任何内容???

HTML 代码

<div id="Dropdown_box">
    <p style="background-color:#456;margin-top:-10px;margin-left:10px;display:inline-block;padding:10px;">Please Select The DropDown Options</p> 
    <p></p>

    <div id="DropDown_List_Main" name="DropDown_List_Main">
    <select Id="DropDown_List_Main" onchange="javascript: dropdown_1(this.options[this.selectedIndex].value);">
        <option value="">Please Select</option>
        <option value="Visitor">Visitor</option>
        <option value="Customer">Customer</option>
        <option value="Company">Company</option>
        <option value="Supplier">Supplier</option>
    </select>
</div>

<div id="DropDown_List_Main" name="DropDown_List_Main">
    <script type="text/Javascript" language="Javascript">
document.write('<select name="DropDown_List" id="DropDown_List" ><option value="">Please Select</option></select>')
    </script>
    <noscript>
        <p>Please Enable Javascript</p>
    </noscript>
</div>

Javascript代码

function DropDown_1(listindex) {
    document.getElementById('DropDown_List').length = 0;
    switch(listindex){
        case "Visitor":
            document.getElementById("DropDown_List").options[0]=new Option("Please Select","");
            document.getElementById("DropDown_List").options[1]=new Option("Question","Question");
            document.getElementById("DropDown_List").options[2]=new Option("Compliment","Compliment");
            document.getElementById("DropDown_List").options[3]=new Option("Request","Request");
            break;

        case "Customer":
            document.getElementById("DropDown_List").options[0]=new Option("Please Select","");
            document.getElementById("DropDown_List").options[1]=new Option("Question","Question");
            document.getElementById("DropDown_List").options[2]=new Option("Request","Request");
            document.getElementById("DropDown_List").options[3]=new Option("Reciept","Reciept");
            document.getElementById("DropDown_List").options[4]=new Option("Complaint","Complaint");
            document.getElementById("DropDown_List").options[5]=new Option("Compliment","Compliment");
            break;

        case "Company":
           document.getElementById("DropDown_List").options[0]=new Option("Please Select","");
           document.getElementById("DropDown_List").options[1]=new Option("Request","Request");
           document.getElementById("DropDown_List").options[2]=new Option("Reciept","Reciept");
           document.getElementById("DropDown_List").options[3]=new Option("Complaint","Complaint");
           document.getElementById("DropDown_List").options[4]=new Option("Compliment","Compliment");
           document.getElementById("DropDown_List").options[5]=new Option("Delivery","Delivery");
           break;

        case "Supplier":
            document.getElementById("DropDown_List").options[0]=new Option("Please Select","");
            document.getElementById("DropDown_List").options[1]=new Option("Request","Request");
            document.getElementById("DropDown_List").options[2]=new Option("Reciept","Reciept");
            document.getElementById("DropDown_List").options[3]=new Option("Complaint","Complaint");
            document.getElementById("DropDown_List").options[4]=new Option("Compliment","Compliment");
            document.getElementById("DropDown_List").options[5]=new Option("Delivery","Delivery");
            break;
    }
    return true;
}

请大家帮忙,那就太好了! 请不要让我引用 JQuery,我是新人或者对 javascript 或多或少是新人 我尝试使用除 JavaScript 之外的任何内容,但只有 PHP 和 mysql 数据库方法 obv 这是表单的一部分,第二个下拉列表需要代码来取消隐藏或创建对应于第一个下拉列表的字段的填充。

对此有任何帮助也很好,否则除了 JQuery 之外我还可以使用其他编程吗?

最佳答案

其实问题出在函数名上。 JavaScript 区分大小写。正如您在 onchange 中提到的那样,它应该是 dropdown_1。

function dropdown_1(listindex) {
    document.getElementById('DropDown_List').length = 0;
switch(listindex){
    case "Visitor":
document.getElementById("DropDown_List").options[0]=new Option("Please Select","");
document.getElementById("DropDown_List").options[1]=new Option("Question","Question");
document.getElementById("DropDown_List").options[2]=new Option("Compliment","Compliment");
document.getElementById("DropDown_List").options[3]=new Option("Request","Request");
break;

    case "Customer":
document.getElementById("DropDown_List").options[0]=new Option("Please Select","");
document.getElementById("DropDown_List").options[1]=new Option("Question","Question");
document.getElementById("DropDown_List").options[2]=new Option("Request","Request");
document.getElementById("DropDown_List").options[3]=new Option("Reciept","Reciept");
document.getElementById("DropDown_List").options[4]=new Option("Complaint","Complaint");
document.getElementById("DropDown_List").options[5]=new Option("Compliment","Compliment");
break;

case "Company":
document.getElementById("DropDown_List").options[0]=new Option("Please Select","");
document.getElementById("DropDown_List").options[1]=new Option("Request","Request");
document.getElementById("DropDown_List").options[2]=new Option("Reciept","Reciept");
document.getElementById("DropDown_List").options[3]=new Option("Complaint","Complaint");
document.getElementById("DropDown_List").options[4]=new Option("Compliment","Compliment");
document.getElementById("DropDown_List").options[5]=new Option("Delivery","Delivery");
break;

case "Supplier":
document.getElementById("DropDown_List").options[0]=new Option("Please Select","");
document.getElementById("DropDown_List").options[1]=new Option("Request","Request");
document.getElementById("DropDown_List").options[2]=new Option("Reciept","Reciept");
document.getElementById("DropDown_List").options[3]=new Option("Complaint","Complaint");
document.getElementById("DropDown_List").options[4]=new Option("Compliment","Compliment");
document.getElementById("DropDown_List").options[5]=new Option("Delivery","Delivery");
break;

}

document.getElementById('DropDown_List').style.display='';
return true;
}
 <div id="Dropdown_box">
<p style="background-color:#456;margin-top:-10px;margin-left:10px;display:inline-block;padding:10px;">Please Select The DropDown Options</p> 
<p></p>

<div id="DropDown_List_Main" name="DropDown_List_Main">
<select Id="DropDown_List_Main" onchange="javascript: dropdown_1(this.options[this.selectedIndex].value);">
    <option value="">Please Select</option>
    <option value="Visitor">Visitor</option>
    <option value="Customer">Customer</option>
    <option value="Company">Company</option>
    <option value="Supplier">Supplier</option>
</select>
</div>

<div id="DropDown_List_Main" name="DropDown_List_Main">
<script type="text/Javascript" language="Javascript">
document.write('<select name="DropDown_List" id="DropDown_List" style="display:none;"><option value="">Please Select</option></select>')
</script>
<noscript>
<p>Please Enable Javascript</p>
</noscript>
</div>

关于javascript - 动态下拉问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45101999/

相关文章:

javascript - 单击提交后如何使表单消失?

javascript - 使用 CSS 生成省略号和 "Read more"链接

javascript - sessionStorage 中的密码

javascript - 通过 github 页面托管时出现问题

javascript - jquery .not() 用于关闭菜单(或模式)不起作用

javascript - 如何通过 TypeScript 在 Next.js 中使用 Peer.js?

javascript - 如何使用复选框实现多个过滤器?

javascript - 如何使 react 带按钮响应?

html - 对外部 svg 使用 preserveAspectRatio

javascript - 我的推送菜单中的 body 动画不起作用