javascript - 选择selectpicker选项后如何选中复选框?

标签 javascript php jquery laravel-5

我是 jquery 新手。我已经完成了一半的工作。但我没有找到任何解决方案。 enter image description here

在这里您可以看到图像有四个复选框“指甲”、“头发”、“皮肤护理”、“按摩”。我希望当用户单击“沙龙”、“移动美容师”和“以上美甲”复选框时,将被选中,而当用户单击“选择您的服务”时,美甲复选框将被取消选中,因此这适用于所有人。我已经这样做了,但问题是,当我第二次从指甲中选择该项目时,复选框未被选中。它只工作一次。请帮我。这里我有代码:-

HTML:-

<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>

我正在使用 Laravel 框架 这是我的 jQuery 代码:-

$('.selectpicker').selectpicker('refresh');
$(".selectpicker").on('change', function() {
    var value = $(this).parents(".div_img_part-2").find(".selectpicker").val();
    alert(value);
    if (value == "") {
        $(this).parents(".div_img_part-2").find("input[type='checkbox']").attr('checked', false);
        if ($("input:checked").length == 0) {
            $('.disable').prop('disabled', false);
            $('.selectpicker').selectpicker('refresh');
        }
    } else {
        $(this).parents(".div_img_part-2").find("input[type='checkbox']").attr('checked', true);
    }
});

最佳答案

使用closest()和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);
    }
});

关于javascript - 选择selectpicker选项后如何选中复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41217886/

相关文章:

javascript - 测试实际视频文件能否播放

php - 我正在尝试在表中添加一个下拉菜单框,其中包含从 mysql 获取的数据

php - mysql中的机票预订查询,基于日期、目的地和返回日期

javascript - 这个结构可以一直工作吗

javascript - 触发补间动画以单独悬停在 SVG 六边形上

javascript - ChartJs Donut 解析数据集

javascript - 在 Meteor JS 应用程序中访问用户电子邮件地址

php - 在 WordPress 安装中发现的这个恶意 PHP 代码有什么作用?

jquery - 为什么我的 jQuery 选择器不起作用?

javascript - jQuery/CSS - 使元素适合屏幕(没有水平滚动条)