简体   繁体   中英

Dropdown-menu width changes when other <li> is added by jQuery

CODE :

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script
    src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"
    integrity="sha256-eEa1kEtgK9ZL6h60VXwDsJ2rxYCwfxi40VZ9E0XwoEA="
    crossorigin="anonymous"></script>
</head>
<body>

<div class="dropdown bigdiv">
  <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
    <span class="caret"></span></button>
  <ul class="dropdown-menu bigul">
    <li>
        <a href="#" id="open_input">open_input</a>
    </li>

    <ul class="list-group test">
    </ul>
  </ul>
</div>

</body>
<script>
    $(document).ready(function () {
        var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "Fortran",
        ];

        var ul_text = '    <li class="ui-widget">\n' +
            '<form role="form" style="">\n' +
            '                <div class="input-group input-group-sm" style="">\n' +
            '                  <input type="text" class="form-control" id="tags" placeholder="Enter email">\n' +
            '                </div>\n' +
            '              </form>\n' +
            '    </li>'

        $('#open_input').click(function (e) {
            $('.bigul').append(ul_text)
            $('#tags').autocomplete({
                    search: function(event, ui) {
                        $('.test').empty();
                    },
                    source: availableTags,
                    messages: {
                        noResults: '',
                        results: function() {}
                    }
                }).data('autocomplete')._renderItem = function(ul, item) {
                    return $('<li class="list-group-item"></li>')
                        .data('item.autocomplete', item)
                        .append(item.value)
                        .appendTo($('.test'));
                    };
            e.stopPropagation();
        });
    });
</script>

jsfiddle : http://jsfiddle.net/uMqyn/919/

On that code, when I click #open_input , ul.dropdown-menu.bigul 's width changes from 160px to 182px. As you can see on , If I click #open_input some <li> is added.

Even I gave style="100%" to all elements of var ul_text , it does not works.

Question:

First Question : How can I make width will be the same as initial status? I want it to have the same width - 160px. It's main question.

Second Question : If it is possible, I want to follow width of <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example button . In other words, if width of button is 20px, dropdown-menu and other following <li> 's width will be 20px width also. But first question is most important to me. Second question is not as important as first one.

you can use .one method:

 $('#open_input').one('click', function(e) {
         $('.bigul').append(ul_text);
 ...

The simple solution, modify this line in your javascript:

<input type="text" class="form-control" id="tags" placeholder="Enter email">

to

<input type="text" class="form-control" id="tags" placeholder="Enter email" style="width:158px;">

You can view it here: http://jsfiddle.net/6ybd6656/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM