Saya menggunakan plugin pelengkapan otomatis terwujud dengan chip. (https://materializecss.com/chips.html#basic)
Pada daftar pelengkapan otomatis saya telah menetapkan gambar untuk setiap opsi. Jadi ketika pengguna memilih opsi pelengkapan otomatis, saya ingin membuat chip dengan gambar yang dipilih. Saya menemukan pertanyaan yang sama persis seperti ini (masukkan deskripsi tautan di sini )
kode:
$(document).ready(function() {
$('.edit--assignee').material_chip({
autocompleteOptions: {
data: {
'Kyle Robinson': 'https://randomuser.me/api/portraits/men/78.jpg',
'Rebecca Smith': 'https://randomuser.me/api/portraits/women/78.jpg',
'Aaron Lloyd': 'https://randomuser.me/api/portraits/men/79.jpg'
},
limit: Infinity,
minLength: 1
}
});
$('.chips').on('chip.add', function(e, chip) {
var data = {
'Kyle Robinson': 'https://randomuser.me/api/portraits/men/78.jpg',
'Rebecca Smith': 'https://randomuser.me/api/portraits/women/78.jpg',
'Aaron Lloyd': 'https://randomuser.me/api/portraits/men/79.jpg'
}
var key = chip.tag;
$(this).children('.chip').last().append('<img src="' + data[key] + '">');
});
});
<div class="edit--assignee">
</div>
Jadi masalah yang saya miliki adalah kode ini untuk materialize versi lama. Saat ini saya menggunakan v1.0 jadi saya mengubah kodenya menjadi:
$(document).ready(function() {
$('.chips-autocomplete').chips({
autocompleteOptions: {
data: {
'Kyle Robinson': 'https://randomuser.me/api/portraits/men/78.jpg',
'Rebecca Smith': 'https://randomuser.me/api/portraits/women/78.jpg',
'Aaron Lloyd': 'https://randomuser.me/api/portraits/men/79.jpg'
},
limit: Infinity,
minLength: 1
},
onChipAdd: function(e, chip) {
var data = {
'Kyle Robinson': 'https://randomuser.me/api/portraits/men/78.jpg',
'Rebecca Smith': 'https://randomuser.me/api/portraits/women/78.jpg',
'Aaron Lloyd': 'https://randomuser.me/api/portraits/men/79.jpg'
}
var key = chip.tag;
alert(chip)
$('.chips').children('.chip').last().append('<img src="' + data[key] + '">');
}
});
});
<div class="chips chips-autocomplete"></div>
juga mencoba:
$(document).ready(function() {
$('.chips-autocomplete').chips({
autocompleteOptions: {
data: {
'Kyle Robinson': 'https://randomuser.me/api/portraits/men/78.jpg',
'Rebecca Smith': 'https://randomuser.me/api/portraits/women/78.jpg',
'Aaron Lloyd': 'https://randomuser.me/api/portraits/men/79.jpg'
},
limit: Infinity,
minLength: 1
},
onChipAdd: function(e) {
var data = {
'Kyle Robinson': 'https://randomuser.me/api/portraits/men/78.jpg',
'Rebecca Smith': 'https://randomuser.me/api/portraits/women/78.jpg',
'Aaron Lloyd': 'https://randomuser.me/api/portraits/men/79.jpg'
}
var chip = M.Chips.getInstance($('.chips-autocomplete')).chipsData;
var key = chip.tag;
alert(chip)
$('.chips').children('.chip').last().append('<img src="' + data[key] + '">');
}
});
});