我有两个选择,它们允许使用引导选择插件服务和Payment_type进行多个选择。我想要的是当服务中的所有选项都被取消选择/取消选中后,payment_type启用。注意:禁用代码可以正常工作,但禁用字段则不能。我在这里做错什么了吗?
此外,payping_type和服务最初被禁用,但是通过选择另一个字段来启用。
<select id="services" class="form-control" multiple="multiple" disabled>
<option value="tire_rotation">Tire Rotation</option>
<option value="oil_change">Oil Change</option>
<option value="brake_check">Brake Check</option>
</select>
<select id="payment_type" class="form-control" multiple="multiple" disabled>
<option value="cash">Cash</option>
<option value="check">Check</option>
<option value="credit_card">Credit Card</option>
<option value="debit_card">Debit Card</option>
</select>
$(document).ready(function() {
$('#services').multiselect({
buttonWidth: '375px',
nonSelectedText: 'Select...',
dropLeft: true,
includeSelectAllOption: true,
numberDisplayed: 1,
onChange: function() {
var servicesSelected = $("#services :selected").length;
if ($(servicesSelected.length != 0)) {
$('#payment_type').multiselect('disable');
$('#payment_type').multiselect("deselectAll", false).multiselect("refresh");
}
else {
$('#payment_type').multiselect('enable');
}
}
});
});
预期结果:取消选中所有服务后,payment_type select启用。
答案 0 :(得分:1)
这将起作用...
$(document).ready(function() {
$('#services').multiselect({
buttonWidth: '375px',
nonSelectedText: 'Select...',
dropLeft: true,
includeSelectAllOption: true,
numberDisplayed: 1,
onSelectAll: function(checked) {
enableDisablePaymentType(checked);
},
onChange: function() {
var servicesSelected = $("#services :selected").length > 0 ? true : false;
enableDisablePaymentType(servicesSelected);
}
});
});
function enableDisablePaymentType(servicesSelected) {
if (servicesSelected) {
$('#payment_type').multiselect('disable');
$('#payment_type').multiselect("deselectAll", false).multiselect("refresh");
} else {
$('#payment_type').multiselect('enable');
}
}