您可以在按钮(如id)和下拉选项中添加一些其他信息。例如:
<div class="btn-group">
<button id='btn' type="button" class="btn btn-danger btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">High</button>
<ul class="dropdown-menu">
<li><a name="priority" data-priority="medium" class="dropdown-item">Medium</a></li>
<li><a name="priority" data-priority="low" class="dropdown-item">Low</a></li>
<li><a name="priority" data-priority="servicerequest" class="dropdown-item">ServiceRequest</a></li>
然后,下一个脚本通过在select中选择option来更改按钮样式和文本(它使用option文本比较,所以不需要数据优先级标记,但您可以重写脚本以使用它)
$('a[name=priority]').on('click',function() {
const btn = $('#btn');
const selectedPrior = $(this);
switchPriority(btn, selectedPrior);
switchBtnStyle(btn);
});
function switchPriority(currentPriorBtn, newPriorA){
const btnText = currentPriorBtn.text();
const aText = newPriorA.text();
newPriorA.text(btnText);
currentPriorBtn.text(aText);
}
function switchBtnStyle(btn){
const btnText = btn.text();
btn.removeClass();
switch(btnText){
case 'High':
btn.addClass('btn btn-danger btn-sm dropdown-toggle');
break;
case 'Low':
btn.addClass('btn btn-success btn-sm dropdown-toggle');
break;
case 'Medium':
btn.addClass('btn btn-warning btn-sm dropdown-toggle');
break;
case 'ServiceRequest':
btn.addClass('btn btn-info btn-sm dropdown-toggle');
break;
}
}
不停摆弄
here