我为自定义下拉列表编写了jQuery方法。点击ul li a
我从标题标签获取id,我通过php代码包含文本,文本添加div和标题添加输入标签,其类型是隐藏的。在服务器上它更改文本但不添加输入隐藏字段
$("#clas_track li a").click(function () {
var selText = $(this).text();
var selvalue = $(this).attr('title');
$(this).parents('.btn-group').find('.dropdown-toggle').html(selText + '<input type="hidden" id="clas_track2" value="' + selvalue + '"/> <span class="caret"></span>');
$.ajax({
url: web_url + 'admin/class/test',
type: 'post',
dataType: 'json',
data: {id: selvalue},
success: function (response) {
$('#clas_courses').find('li').remove().end();
var toAppend = '';
for (var i = 0; i < response.length; i++) {
toAppend += '<li ><a title="' + response[i]['course_id'] + '" href="#">' + response[i]['course_title'] + '</a></li>';
}
$('#clas_courses').append(toAppend);
},
error: function () {
alert('Network Error');
}
});
});
在html中:
<div class="cmn_select select_sm">
<div class="btn-group">
<a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#" aria-expanded="false">This is a New Track <span class="caret"></span></a>
<ul class="dropdown-menu" id="clas_track">
<li><a title="1" href="#">track3332 Testing</a> </li>
<li><a title="5" href="#">live2</a> </li>
<li><a title="8" href="#">Track</a> </li>
<li><a title="9" href="#">This is a New Track</a> </li>
<li><a title="10" href="#">User Tack</a> </li>
<li><a title="11" href="#">Track one</a> </li>
<li><a title="12" href="#">Track two</a> </li>
</ul>
</div>
</div>