Javascript 选择selectpicker选项后如何选中复选框?
我是jquery新手。我已经做了一半的工作。但我没有找到任何解决办法 这里你看到的图像有四个复选框:指甲、头发、皮肤护理、按摩。我想当用户点击沙龙,移动美容师和以上两个指甲复选框将被选中,当用户点击选择您的服务指甲复选框将被取消选中,所以这适用于所有。我已经这样做了,但问题是当我第二次从钉子中选择项目时,复选框并没有被选中。它只工作一次。请帮帮我。我这里有代码:- Html:-Javascript 选择selectpicker选项后如何选中复选框?,javascript,php,jquery,laravel-5.2,Javascript,Php,Jquery,Laravel 5.2,我是jquery新手。我已经做了一半的工作。但我没有找到任何解决办法 这里你看到的图像有四个复选框:指甲、头发、皮肤护理、按摩。我想当用户点击沙龙,移动美容师和以上两个指甲复选框将被选中,当用户点击选择您的服务指甲复选框将被取消选中,所以这适用于所有。我已经这样做了,但问题是当我第二次从钉子中选择项目时,复选框并没有被选中。它只工作一次。请帮帮我。我这里有代码:- Html:- <div class="one-row"> <?php foreach ($services
<div class="one-row">
<?php foreach ($services as $key => $allservices) {
if ($key <= 3) {
if (!empty($data['services'])) {
if (in_array($allservices['id'], $data['services'])) {
$checked = "checked";
} else {
$checked = "";
}
} else {
$checked = "";
} ?>
<div class="div_img_part-2">
<span class="img_part_class-2"><img src="{{ asset('images/ServiceImages/'. $allservices['image'])}}">
</span>
<span class="text_part_class-2">
<p class="custom-checkbox firstpart">
<input class="firstdisable" type="checkbox" id="{{ $key }}" name="services[]"
value="{{ $allservices['id'] }}" <?= $checked; ?>/>
<label for="{{ $key }}">{{$allservices['name']}}</label>
/p>
</span>
</span>
<select name="service_type[<?php echo $allservices['name']; ?>]" class="selectpicker">
<option value="">Select Your Sevice</option>
<option value="Salon" <?php if (!empty($data['service_type'][$allservices['name']])) {
if ($data['service_type'][$allservices['name']] == "Salon") { ?> selected
<?php }
} ?> >Salon
</option>
<option value="Mobile beautician" <?php if (!empty($data['service_type'][$allservices['name']])) {
if ($data['service_type'][$allservices['name']] == "Mobile beautician") { ?> selected
<?php }
} ?> >Mobile beautician
</option>
<option value="Both" <?php if (!empty($data['service_type'][$allservices['name']])) {
if ($data['service_type'][$allservices['name']] == "Both") { ?> selected
<?php }
} ?>>Both
</option>
</select>
</div>
<?php }
} ?>
</div>
使用最近的()和prop()而不是parent()和attr()
试试这个:
$(".selectpicker").on('change', function () {
var value = $(this).val();
if (value == "") {
$(this).closest(".div_img_part-2").find("input[type='checkbox']").prop('checked', false);
if ($("input:checked").length == 0) {
$('.disable').prop('disabled', false);
$('.selectpicker').selectpicker('refresh');
}
} else {
$(this).closest(".div_img_part-2").find("input[type='checkbox']").prop('checked', true);
}
});
使用.prop()而不是attr()来选中/取消选中复选框。
$(".selectpicker").on('change', function () {
var value = $(this).val();
if (value == "") {
$(this).closest(".div_img_part-2").find("input[type='checkbox']").prop('checked', false);
if ($("input:checked").length == 0) {
$('.disable').prop('disabled', false);
$('.selectpicker').selectpicker('refresh');
}
} else {
$(this).closest(".div_img_part-2").find("input[type='checkbox']").prop('checked', true);
}
});