php - 单击 Bootstrap 下拉元素并更改值

标签 php jquery twitter-bootstrap

这是 dropdwon

<div class="col-md-6">
             <div class="dropdown">
                <input  type="text" data-toggle="dropdown" id="search-input" class="form-control">
                <ul class="dropdown-menu" id="employee-list">
                  <!--filled by ajax -->
                </ul>
              </div>
        </div>

我用 a 获取列表,并尝试获取点击事件,但没有完成任何操作

$(document).ready( function(){
    $('#search-input').keyup(function(e){
        if($(this).val().length >= 3 /*&& e.which != 8*/ ){
            get_users($(this).val());
        }
    });

    $(".dropdown-menu li a").click(function(){
        alert('aaaa');
    });
});

我需要单击列表元素并使用单击元素的文本设置输入文本,但我无法处理单击事件

function get_users($text){
    $.ajax({
      url: "asociate_ci.php",
      method: 'get',
      data: {
        'text': $text ,
        'option':'get_users' 
      }
    })
      .done(function( response ) {
        var employees = JSON.parse(response);
        //console.dir(employees);
        $('#employee-list').empty();
        $.each(employees, function(){
            //console.log(this.last_name);
            $('#employee-list').append("<li ><a class='employee-li'>"+this.last_name+"</a></li>");
        });
        $('#search-input').focus();
      });
}

最佳答案

您需要对与选择器 .dropdown-menu 匹配的所有元素使用 on,并使用单个处理程序 a。这将能够绑定(bind)到动态创建的元素。

// $(".dropdown-menu") = watch for any changes to the dom inside this element
// 'click' = event type
// a = add click event to these elements
$('.dropdown-menu').on('click', 'a', function(){
    alert('aaaa');
});

关于php - 单击 Bootstrap 下拉元素并更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44432583/

相关文章:

php - 仅根据需要在 PHP 数组中添加项目

php - 不使用 fetch 检查空字段

Jquery 对话框 : content shown before the dialog is open

php - 显示每个国家和城市的联系人

PHP gettext 不起作用

使用非侵入式验证时 jQuery 验证未执行

javascript - mouseenter mouseleave 和一个选择

css - Bootstrap 4 使其固定宽度(使其无响应)

css - Bootstrap 网格重新布局

twitter-bootstrap - 使用 Bootstrap 在两列中显示复选框