オプションの選択に基づいてデータが追加されるテーブルがあります。問題は、データを複数回追加しないようにすることです。
オプションcolor
データの追加を選択size
してから、これまでのところオプションデータの追加も選択するとします。
しかし、もう一度間違って選択color
した場合は。カラーデータが再び表示されます。これは基本的に、これらのデータをバックエンドで送信したい瞬間まで問題はありません。同じIDを異なる値でフィルタリングするのは困難です。
この重複した追加を回避する方法はありますか?では、1color
回選択すると、データは1回だけ表示され、2回目は表示されませんか?
HTML
<div class="mt-20 options" style="display:none">
<div class="mb-20"><h4>Check mark your needed options only</h4></div>
<table id="table" class="table table-bordered table-hover table-responsive">
<thead>
<th width="50" class="text-center">Check</th>
<th class="text-center">Title</th>
<th class="text-center">Price</th>
</thead>
<tbody>
</tbody>
</table>
</div>
JavaScript
<script>
$(function(){
$('select[name="options"]').on('change', function() {
var addressID = $(this).val();
if(addressID) {
$.ajax({
url: '{{ url('admin/getoptions') }}/'+encodeURI(addressID),
type: "GET",
dataType: "json",
success:function(data) {
// $('div.options').empty();
// 2. Loop through all entries
var keys = ['title'];
data.forEach(function(row) {
var $row = $('<tr />');
$row.append('<td class="text-center" width="50"><label class="switch switch-small"><input type="checkbox" /><span><input class="form-control" type="text" name="optionID[]" value="'+row['id']+'"></span></label></td>');
keys.forEach(function(key) {
$row.append('<td>' + row[key] + '</td>');
});
$row.append('<td class="text-center"><input class="form-control" placeholder="if fill this price, the price will add to product price when user select it." type="number" name="optionPRICE[]"></td>');
$('#table tbody').append($row);
});
$("div.options").show();
}
});
}else{
$('div.options').empty();
}
});
});
</script>
それについて何か考えはありますか?
もちろん。最初は空の配列を作成できます。
var addresses = [];
アドレスを要求したときはいつでも、次のように配列内に格納します。
addresses.push(addressID);
次のように、ifを変更して、addressID
真でまだ要求されていないかどうかを確認します。
if (addressID && (addresses.indexOf(addressID) < 0)) {
完全なコード:
<script>
$(function(){
var addresses = [];
$('select[name="options"]').on('change', function() {
var addressID = $(this).val();
if (addresses.indexOf(addressID) < 0) {
if(addressID) {
addresses.push(addressID);
$.ajax({
url: '{{ url('admin/getoptions') }}/'+encodeURI(addressID),
type: "GET",
dataType: "json",
success:function(data) {
// $('div.options').empty();
// 2. Loop through all entries
var keys = ['title'];
data.forEach(function(row) {
var $row = $('<tr />');
$row.append('<td class="text-center" width="50"><label class="switch switch-small"><input type="checkbox" /><span><input class="form-control" type="text" name="optionID[]" value="'+row['id']+'"></span></label></td>');
keys.forEach(function(key) {
$row.append('<td>' + row[key] + '</td>');
});
$row.append('<td class="text-center"><input class="form-control" placeholder="if fill this price, the price will add to product price when user select it." type="number" name="optionPRICE[]"></td>');
$('#table tbody').append($row);
});
$("div.options").show();
}
});
}else{
$('div.options').empty();
}
}
});
});
</script>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加