好的,有一個問題。該數組不保存來自輸入的數據。當我想保存一個名字時,它仍然是空的,我不能輸出任何東西。在我爲其他輸入編寫代碼之前,我只測試一個名稱輸入以確保一切正常。任何幫助和建議將不勝感激。JS |數組不會保存來自輸入|的數據解?
$(document).ready(function() {
$('#newContact').on('click', function() {
$('#dataSection').slideToggle();
$('#buttonSave').delay(400).fadeToggle();
});
});
var contacts = [];
function printPerson(person) {
console.log(person.firstName);
};
var firstName = document.getElementById("firstName").value;
function addFirstName(firstName) {
this.firstName = firstName;
};
function saveContact() {
contacts[contacts.length] = new addFirstName(document.getElementById("firstName").value);
document.getElementById("output").innerHTML = contacts[0];
};
.input-group {
margin: 2px;
padding: 2px;
}
#newContact:hover {
cursor: pointer;
}
#dataSection, #buttonSave {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AddressBookApp</title>
<link rel="stylesheet" type="text/css" href="addressbook.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h2>AddressBook App</h2>
<hr class="my-4">
<h5><span class="badge badge-secondary" id="newContact">New contact</span></h5>
<!-- Contact section -->
<div class="alert alert-success" role="alert" id="dataSection">
<!-- Firstname Input -->
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">1</span>
<input type="text" class="form-control" placeholder="Type a first name" aria-label="Name" id="firstName">
</div>
<!-- LastName Input -->
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">2</span>
<input type="text" class="form-control" placeholder="Type a last name" aria-label="Lastname" id="lastName">
</div>
<!-- PhoneNumber Input -->
<div class="input-group">
<span class="input-group-addon" id="basic-addon3">3</span>
<input type="text" class="form-control" placeholder="Type a number phone" aria-label="Number" id="phoneNumber">
</div>
<!-- Email Input -->
<div class="input-group">
<span class="input-group-addon" id="basic-addon4">4</span>
<input type="text" class="form-control" placeholder="Type an email">
</div>
</div>
<!-- Button save provided contact -->
<button onclick="saveContact()" type="button" class="btn btn-info" id="buttonSave">Save</button>
<p id="output"></p>
</div>
<!-- Scripts -->
<!-- Jquery -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<!-- Bootstrap -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- JS -->
<script type="text/javascript" src="addressbook.js"></script>
</body>
</html>
'聯繫人[0]'是一個對象,你要訪問它的屬性'聯繫人[0] .firstName' – Lixus