我在 laravel 中使用 select2(多選)下拉選單。
在這里,我使用的是萬維網選項,它單擊所有選項都被選中。但是萬維網也被選中了我只希望選項值和選擇不是萬維網(全選)選項出現在選擇框中。如何解決?
<div class="col-md-8">
<div class="form-group">
<label for="service_continent">Service Continent</label>
<div class="select2-purple">
<select id="service_continent" class="form-control form-select form-select-lg col-lg-12 service_continent" multiple="multiple" aria-label=".form-select-lg example" data-dropdown-css-class="select2-purple" name="service_continent[]" style="height:50px !important;">
<!-- <input type="checkbox" >Select All -->
<option value=''>Select Continent</option>
<option value="World Wide">World Wide</option>
@foreach($continents as $index => $continent)
<!-- <option value="{{ $index }}" @if(isset($company) && $company->service_continent == $index || old('service_continent')===$index) selected @endif>{{ $continent }}</option> -->
<option value="{{ $index }}" @if(isset($company) && in_array($index,$serviceContinent)|| (old('service_continent') && in_array($index, old('service_continent')))) selected @endif>{{ $continent }}</option>
@endforeach
</select>
@if ($errors->has('service_continent'))
<span class="text-danger">{{ $errors->first('service_continent') }}</span>
@endif
</div>
</div>
</div>
var service_continent = $('.service_continent').select2();
$('.service_continent').on("select2:select", function (e) {
var data = e.params.data.text;
if(data=='World Wide'){
$(".service_continent > option").prop("selected","selected");
$(".service_continent").trigger("change");
}
});
uj5u.com熱心網友回復:
這將幫助你!
$('.service_continent').select2();
$('.service_continent').on('change', function(event) {
event.preventDefault();
if ($(this).val() == 'all') {
// select all options except first one
$('.service_continent > option:not(:first)').prop('selected', true);
// unselect "Select All" option
$('.service_continent > option:first').prop('selected', false);
$('.service_continent').trigger('change');
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418582.html
標籤:
上一篇:單擊單選按鈕選擇下拉值
下一篇:如何用倒計時重繪特定的div
