简体   繁体   中英

html select tag change from checked checkbox

how can i do when some option is checked in checkbox, that label showing in select tag? 在此处输入图片说明

Checkbox input

<input
type="checkbox"
class="toggle-product pull-right" @if(!isset($restrictedProductIds[$p->id])) checked
@endif
data-url="{{route('image.settings.product.toggle',[$account,$album,$image,$p])}}">

Select tag

                    <select name="default_id" id="default_id" class="form-control">
                    <option value="{{$image->default_product_id}}" selected>{{$image->default_product_id}}</option>
                    @foreach($validProducts as $product)
                        @if(!isset($restrictedProductIds[$product->id]))
                        <option value="{{$product->id}}">
                            {{$product->getTranslatedName()}}
                        </option>
                        @endif
                    @endforeach
                </select>

class .toggle-product

  $(".toggle-product").on('click', function () { var checked = $(this).is(':checked'); var $status = $(this).parents(".list-group-item"); $status.addClass('list-group-item-warning'); $.post($(this).data('url'), { 'enabled': checked ? 1 : 0, '_token': "{{csrf_token()}}" }, function (response) { $status.removeClass('list-group-item-warning'); //location.reload(); }.bind(this)) }); 

Try this sample code, you can use it in your own way.

 $(document).ready(function() { $(document).on("change", "#select-ele", function() { var cur_val = $(this).val(); $("[name='check-ele']").attr("checked", false); if (cur_val.length > 0) { $("[name='check-ele']").each(function() { var check_val = $(this).val(); if ($.inArray(check_val, cur_val) > -1) { $(this).attr("checked", true); } }); } }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <select id="select-ele" multiple> <option value="1">Canvas</option> <option value="2">Acrylic</option> <option value="3">Cushion</option> <option value="4">Mug</option> </select> <br/> <input type="checkbox" name="check-ele" value="1">Canvas <input type="checkbox" name="check-ele" value="2">Acrylic <input type="checkbox" name="check-ele" value="3">Cushion <input type="checkbox" name="check-ele" value="4">Mug 

I have used $.inArray to check if checkbox value is selected in dropdown values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM