html选中标签更改复选框

时间:2017-04-12 07:44:14

标签: javascript php jquery html laravel

如果勾选复选框中的某个选项,该标签会显示在选择标记中,该怎么办? enter image description here

复选框输入

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

选择标记

            $(".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))
            });

class .toggle-product

&#13;
&#13;
var newDate = new Date();  
console.log("0" + newDate.getHours());
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

试试这个示例代码,您可以按照自己的方式使用它。

&#13;
&#13;
$(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);
        }
      });
    }
  });
});
&#13;
<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
&#13;
&#13;
&#13;

我已使用$.inArray检查是否在下拉列表中选中了复选框值。