[英]Index in array showing undefined?
由於某種原因,索引號在嘗試從 allcontacts 數組拼接時顯示未定義,你知道為什么會這樣嗎? 謝謝你。
$(document).ready(function() {
let allcontacts = [];
let index = 0;
$(document).on('click', "#addcontact", function() {
let contact = [];
index++;
const eachemail = $(this).closest('#contactdiv').find('#emailinput').not(this).val();
const eachname = $(this).closest('#contactdiv').find('#nameinput').not(this).val();
const eachtype = $(this).closest('#contactdiv').find('#contacttype').not(this).val();
contact.push(eachemail, eachname, eachtype);
allcontacts.push(contact);
});
$(document).on('click', "#removecontact", function() {
allcontacts.splice(index);
$(this).closest('#contactdiv').remove();
});
});
數組中的第一個值的索引為“0”,因此如果數組為空,則索引為“-1”。
$(document).ready(function() {
let allcontacts = [];
let index = -1;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.