Php 单击bootstrap下拉元素并更改值
这是dropdwonPhp 单击bootstrap下拉元素并更改值,php,jquery,twitter-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"
<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>
我需要单击列表元素并将输入文本设置为单击元素的文本,但我无法处理单击事件
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();
});
}
函数获取用户($text){
$.ajax({
url:“asociate_ci.php”,
方法:“get”,
数据:{
“text”:$text,
“选项”:“获取用户”
}
})
.完成(功能(响应){
var employees=JSON.parse(响应);
//总经理(雇员);
$(“#员工列表”).empty();
$。每个(员工,职能(){
//console.log(这个姓);
$(“#员工列表”)。追加(“”+此。姓氏+” ”;
});
$(“#搜索输入”).focus();
});
}
对于与选择器匹配的所有元素,您需要使用on
使用单个处理程序a
。下拉菜单。这将能够绑定到动态创建的元素
// $(".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');
});
当前元素上的jquery处理程序工作正常, 但是新元素没有绑定到这些处理程序,因此我们需要使用.on()方法 更改单击事件,如下所示:
$('.dropdown-menu').on('click', 'a', function(){
alert('aaaa');
});
使用
$(document).on('click',”。下拉菜单li a“,function(){…})代码>随着元素的动态添加。请检查我的答案。由于性能问题,最好使用特定的选择器,而不是整个文档。这家伙解释得很好。我想你已经意识到了这一点,我只是想我会为其他人提一下。
$('.dropdown-menu').on('click', 'a', function(){
alert('aaaa');
});