[英]checkbox checked when value same with data from database-javascript
hi everyone please help, i'm new in javascript, i have checkbox with random value, so i want to when there is data from database same with value in checkbox, checkbox will checked,大家好,请帮忙,我是 javascript 的新手,我有随机值的复选框,所以我想当数据库中的数据与复选框中的值相同时,复选框将被选中,
this is my checkbox,这是我的复选框,
@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
and this my js这是我的 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);
}
});
});
sorry for bad english.抱歉英语不好。
then use this然后用这个
$('.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.