簡體   English   中英

當值與來自 database-javascript 的數據相同時選中復選框

[英]checkbox checked when value same with data from database-javascript

大家好,請幫忙,我是 javascript 的新手,我有隨機值的復選框,所以我想當數據庫中的數據與復選框中的值相同時,復選框將被選中,

這是我的復選框,

@foreach ($loin as $loin)
  <div class="custom-control custom-checkbox">
     <input class="custom-control-input loincode" type="checkbox" id="{{$loin->sap_code}}" name="sap_code[]" 
       value="{{$loin->sap_code}}">
     <label for="{{$loin->sap_code}}" class="custom-control-label">{{$loin->sap_code}} 
     </label>
   </div>
 @endforeach

這是我的 js

$('.tampilModalUbahPts').on('click',function () {
    var id = $(this).data('id');
    $('.modal-body form').attr('action','/pts/'+id+'/update');
    $.ajax({
        url: '/ptspt/getubah',
        data: {id : id},
        method:'get',
        dataType : 'json',
        success: function (data) {
            $('#lbs').val(data.lbs);
            $('.loincode').attr("checked",data.loin== value);
        }
    });
});

抱歉英語不好。

然后用這個

$('.loincode').each(function( index ) {
    $(this).attr("checked",(data.loin && data.loin.filter(x=>(x==$(this).val())).length) ? true : false);
});

 var data={loin:["108591","108592"]} $('.loincode').each(function( index ) { $(this).attr("checked",(data.loin && data.loin.filter(x=>(x==$(this).val())).length)? true: false); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" class="loincode" value="108591" /> <input type="checkbox" class="loincode" value="108592" /> <input type="checkbox" class="loincode" value="108593" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM