我需要仅选中复选框和最多3个数据,因此取消选中复选框无法插入我的数据库
这是桌子
<table class="table table-bordered" id="poTb">
<tbody>
<tr class="text-center">
<th>Pilih</th>
<td>Kode Obat</td>
<td>Nama Obat</td>
<td>Satuan</td>
<td>Harga Obat</td>
<td>Jumlah Obat PO</td>
<td>Total bayar</td>
</tr>
@foreach ($obat as $o)
<tr>
<td>
<input id="check" type="checkbox" name="select[]" value="{{ $o->id }}" id="check">
</td>
<td>
<input type="text" id="kode" class="form-control" name="kode_obat" value="{{ $o->kode_obat }}" readonly>
</td>
<td>
<input type="text" class="form-control" name="nama_obat[]" value="{{ $o->nama_obat }}" readonly>
</td>
<td width="15%">
<div class="form-group">
<select name="satuanp[]" class="form-control">
<option value="" hidden>{{ $satuan[0]->nama_satuan }}</option>
@foreach ($satuan as $s)
<option value="{{ $s->nama_satuan }}">{{ $s->nama_satuan }}</option>
@endforeach
</select>
</div>
</td>
<td>
<input id="harga-{{ $o->id }}" type="text" class="form-control" name="harga_obat[]" value="{{ $o->harga_obat }}" onkeyup="sum({{ $o->id }})">
</td>
<td>
<input id="jumlah-{{ $o->id }}" type="text" class="form-control" name="jumlah[]" onkeyup="sum({{ $o->id }})" value="{{ old('jumlah') }}">
</td>
<td>
<input id="total-{{ $o->id }}" type="text" class="form-control total-harga" name="total_harga[]" value="{{ old('total_harga') }}" onkeyup="sum({{ $o->id }})">
</td>
</tr>
@endforeach
</tbody>
</table>
<button type="submit" class="btn btn-success simpan simp-po" id="addPO"><i class="fas fa-check"> Simpan</i></button>
这是我尝试过的Jquery,因此,如果没有选中的复选框,则会出现警报“您必须至少单击1”,如果选中的复选框大于3,则会出现警报“最大已选中3”,如果至少有1-3个选中的复选框,数据将被插入到数据库中
$(document).ready(function () {
$('#addPO').on('click', function () {
var checked = $('#poTb').find(':checked').length;
if (checked == 0) {
alert('You must click at least 1 !');
} else if( checked > 3){
alert('Maximum 3 checked!');
}else
alert('pass');
});
});
更新 其实我的模型中有这个字段作为数组
protected $casts = [
'select'=>'array',
'jumlah'=>'array',
'harga_obat'=>'array',
'total_harga'=>'array',
'satuan'=>'array',
'nama_obat'=>'array',
];
答案 0 :(得分:0)
您可以这样做-var checked = $('#poTb input:checked').length
$(document).ready(function () {
$('#addPO').on('click', function () {
var checked = $('#poTb input:checked').length;
if (checked == 0) {
alert('You must click at least 1 !');
} else if( checked > 3){
alert('Maximum 3 checked!');
}else
alert('pass');
});
});
答案 1 :(得分:0)
尝试“ $('input [name =” select []“]:checked')。length;”这可以为您提供帮助。
$(document).ready(function () {
$('#addPO').on('click', function () {
var checked = $('input[name="select[]"]:checked').length;
if (checked == 0) {
alert('You must click at least 1 !');
} else if( checked > 3){
alert('Maximum 3 checked!');
}else
alert('pass');
}
});
<table class="table table-bordered" id="poTb">
<tbody>
<tr class="text-center">
<th>Pilih</th>
<td>Kode Obat</td>
<td>Nama Obat</td>
<td>Satuan</td>
<td>Harga Obat</td>
<td>Jumlah Obat PO</td>
<td>Total bayar</td>
</tr>
@foreach ($obat as $o)
<tr>
<td>
<input id="check" type="checkbox" name="select[]" value="{{ $o->id }}" id="check">
</td>
<td>
<input type="text" id="kode" class="form-control" name="kode_obat" value="{{ $o->kode_obat }}" readonly>
</td>
<td>
<input type="text" class="form-control" name="nama_obat[]" value="{{ $o->nama_obat }}" readonly>
</td>
<td width="15%">
<div class="form-group">
<select name="satuanp[]" class="form-control">
<option value="" hidden>{{ $satuan[0]->nama_satuan }}</option>
@foreach ($satuan as $s)
<option value="{{ $s->nama_satuan }}">{{ $s->nama_satuan }}</option>
@endforeach
</select>
</div>
</td>
<td>
<input id="harga-{{ $o->id }}" type="text" class="form-control" name="harga_obat[]" value="{{ $o->harga_obat }}" onkeyup="sum({{ $o->id }})">
</td>
<td>
<input id="jumlah-{{ $o->id }}" type="text" class="form-control" name="jumlah[]" onkeyup="sum({{ $o->id }})" value="{{ old('jumlah') }}">
</td>
<td>
<input id="total-{{ $o->id }}" type="text" class="form-control total-harga" name="total_harga[]" value="{{ old('total_harga') }}" onkeyup="sum({{ $o->id }})">
</td>
</tr>
@endforeach
</tbody>
</table>
<button type="submit" class="btn btn-success simpan simp-po" id="addPO"><i class="fas fa-check"> Simpan</i></button>