ฉันกำลังใช้การเติมข้อความอัตโนมัติที่เป็นรูปธรรมด้วยปลั๊กอินชิป (https://materializecss.com/chips.html#basic)
ในรายการเติมข้อความอัตโนมัติ ฉันได้ตั้งค่ารูปภาพสำหรับแต่ละตัวเลือกแล้ว ดังนั้นเมื่อผู้ใช้เลือกตัวเลือกในการเติมข้อความอัตโนมัติ ฉันต้องการสร้างชิปด้วยรูปภาพที่เลือก ฉันพบคำถามเดียวกันทุกประการเช่นนี้ (ป้อนคำอธิบายลิงก์ที่นี่ )
รหัส:
$(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>
ดังนั้นปัญหาที่ฉันมีคือรหัสนี้มีไว้สำหรับเวอร์ชันเก่าที่เป็นรูปธรรม ขณะนี้ฉันใช้ v1.0 ดังนั้นฉันจึงเปลี่ยนรหัสเป็น:
$(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>
พยายามด้วย:
$(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] + '">');
}
});
});