使用javascript创建的按钮单击下拉列表

时间:2019-01-11 22:50:09

标签: javascript html drop-down-menu

我单击一个按钮,它会在更改时显示一个下拉菜单,我想将其取值并替换为输入字段,但是如果我在返回第一个下拉菜单并更改其值时创建了很多下拉菜单,则输入字段的值不能更改,因为它取决于按钮的单击次数。

            <button type="button" id="buttonadd" onclick="addMore()">Add</button>
            <div id='container'> 
            </div>
            <script>
               var i = 0;
               var x = 0;
               var selectid = 0;
               var input2;
               var select;
               var button;
               var select_id;
               var input2_hiddenID;
               var input2hidden;
               var counter = 0;
               var select_value;
            function addMore()
            {       

                       if(counter >= 1 && (!select_value || select_value == 0)){
                        $("#buttonadd").prop('disabled', true);

                    }else{

                        select_value = 0;
                        button = document.getElementById('container');

                        select  = document.createElement("select");
                        select.options.add( new Option("...select...","0", true, true) );
                        select.options.add( new Option("name","1") );
                        select.options.add( new Option("phone","2") );
                        select.setAttribute("id","selectid" + selectid);

                        select_id = "selectid";
                        select.setAttribute("onchange", "getValue(select_id, selectid)");
                        button.appendChild(select);
                        input2 = document.createElement("input");

                        input2.setAttribute("type","hidden");
                        input2.setAttribute("id","x" + x);
                        input2.setAttribute("name","x" + x);
                        button.appendChild( input2 );

                        var linebreak1 = document.createElement("br");
                        var linebreak2 = document.createElement("br");
                        button.append(linebreak1, linebreak2);
                        i++;
                        x++;
                        selectid++; 
                        counter++;
                    }       
            }

            function getValue(selectid, a){

                    var lastId = a-1;

                    var data = selectid + lastId;

                    select_value = 
             document.getElementById(data).value;

                    var x= "x"+lastId;
                    input2hidden = document.getElementById(x);

                if(counter >= 1 && (!select_value || select_value == 0) ){

                    alert("Please choose from the select menu");
                    $("#buttonadd").prop('disabled', true);

                    input2hidden.value = select_value;

                }else {

                    $("#buttonadd").prop('disabled', false);

                        input2hidden.value = select_value;
                }
            }

1 个答案:

答案 0 :(得分:0)

我不知道我是否很好地理解了您的问题,但是给了您一个解决方法,让我一键了解下拉菜单

**

  

HTML

**

<html>
<head>
<title>DropDown</title>
</head>
<body>
<ul class="nav">
      <li>
        <a href="#">
          No dropdown
        </a>
      </li>
      <li class="button-dropdown">
        <a href="javascript:void(0)" class="dropdown-toggle">
          Dropdown 1 <span>▼</span>
        </a>
        <ul class="dropdown-menu">
          <li>
            <a href="#">
              Drop Item 1
            </a>
          </li>
          <li>
            <a href="#">
              Drop Item 2
            </a>
          </li>
          <li>
            <a href="#">
              Drop Item 3
            </a>
          </li>
        </ul>
      </li>
      <li>
        <a href="#">
          No dropdown
        </a>
      </li>
      <li class="button-dropdown">
        <a href="javascript:void(0)" class="dropdown-toggle">
          Dropdown 2 <span>▼</span>
        </a>
        <ul class="dropdown-menu">
          <li>
            <a href="#">
              asdf
            </a>
          </li>
        </ul>
      </li>
    </ul>


    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

**

  

CSS

**

body {
    background-color: #eee;
    text-align: center;
    padding-top: 50px;
}

.nav {
    display: block;
    font: 13px Helvetica, Tahoma, serif;
    text-transform: uppercase;
    margin: 0; 
    padding: 0;
}

.nav li {
    display: inline-block;
    list-style: none;
}

.nav .button-dropdown {
    position: relative;
}

.nav li a {
    display: block;
    color: #333;
    background-color: #fff;
    padding: 10px 20px;
    text-decoration: none;
}

.nav li a span {
    display: inline-block;
    margin-left: 5px;
    font-size: 10px;
    color: #999;
}

.nav li a:hover, .nav li a.dropdown-toggle.active {
    background-color: #289dcc;
    color: #fff;
}

.nav li a:hover span, .nav li a.dropdown-toggle.active span {
    color: #fff;
}

.nav li .dropdown-menu {
    display: none;
    position: absolute;
    left: 0;
    padding: 0;
    margin: 0;
    margin-top: 3px;
    text-align: left;
}

.nav li .dropdown-menu.active {
    display: block;
}

.nav li .dropdown-menu a {
    width: 150px;
}

**

  

JavaScript

**

jQuery(document).ready(function (e) {
    function t(t) {
        e(t).bind("click", function (t) {
            t.preventDefault();
            e(this).parent().fadeOut()
        })
    }
    e(".dropdown-toggle").click(function () {
        var t = e(this).parents(".button-dropdown").children(".dropdown-menu").is(":hidden");
        e(".button-dropdown .dropdown-menu").hide();
        e(".button-dropdown .dropdown-toggle").removeClass("active");
        if (t) {
            e(this).parents(".button-dropdown").children(".dropdown-menu").toggle().parents(".button-dropdown").children(".dropdown-toggle").addClass("active")
        }
    });
    e(document).bind("click", function (t) {
        var n = e(t.target);
        if (!n.parents().hasClass("button-dropdown")) e(".button-dropdown .dropdown-menu").hide();
    });
    e(document).bind("click", function (t) {
        var n = e(t.target);
        if (!n.parents().hasClass("button-dropdown")) e(".button-dropdown .dropdown-toggle").removeClass("active");
    })
});