我有一个复选框,当它更改(选中/取消选中)时,它将影响下拉菜单。 这是我的复选框和下拉菜单:
<div class="col-sm-2">
<?php
echo Form::widget([
'model' => $model,
'form' => $form,
'attributes' => [
'isMultiSeat' => [
'type' => Form::INPUT_CHECKBOX,
'options' => [
'id' => 'is-multi-seat-id'
]
],
]
]);
?>
</div>
<div class="col-sm-4">
<?php
$base = new BaseController();
echo Select2::widget([
'model' => $model,
'attribute' => 'seatNumber',
'data' => $base->selectSeat($tripId),
'disabled' => false,
'options' => [
'value' => $seatNumber,
'placeholder' => 'Select Seat...',
'multiple' => true,
'id' => 'seat-number-options-id'
]
]);
?>
</div>
我设置了使用 jquery 更改的下拉菜单。这是:
$(document).ready(function () {
$('#is-multi-seat-id').click(function () {
var checkbox = document.getElementById('is-multi-seat-id');
let searchParams = new URLSearchParams(window.location.search);
let param = searchParams.get('id');
var statusSeatOptions = '';
if (checkbox.checked) {
statusSeatOptions = true;
} else {
statusSeatOptions = false;
}
document.getElementById('seat-number-options-id').disabled = statusSeatOptions;
$('#seat-number-options-id').load('create?id=' + param + ' #seat-number-options-id');
});
});
我需要的是,当复选框从未选中变为选中时,下拉列表设置为disable=false
。当复选框从已选中变为未选中时,下拉菜单设置为disable=true
。
我的问题是,如果我在页面刚加载时第一次单击复选框,则下拉禁用状态不会更改。但是,第二次单击后,下拉状态将更改,依此类推。