나를 미치게 만드는 Javascript 문제가 있습니다. 양식을 생성자 함수에 연결하려고합니다. 입력 필드의 값을 가져 와서 새 Person 객체의 속성과 동일하게 설정해야합니다.Javascript : 양식 입력을 인수로 사용하는 생성자를 만들려고합니다.
문제는 제출시 빈 문자열과 동일한 속성을 설정하는 것이므로 변경 사항이 실시간 값으로 업데이트되고 있음에도 불구하고 이유를 알 수 없습니다.
스크립트
//will hold an array of people objects
var list = [];
//constructor that builds new people to add to address book
function Person(first, last, address, number, email) { //new person constructor
this.firstName = first;
this.lastName = last;
this.address = address;
this.number = number;
this.email = email;
};
// When the submit button is clicked, create a new person and add the values of
// the form fields to the properties of the object
$("#addform").submit(function (e) {
e.preventDefault();
var person = new Person($("input[name = 'fname']").val(),
$("input[name = 'lname']").val(),
$("input[name = 'email']").val(),
$("input[name = 'address']").val(),
$("input[name = 'phone']").val());
list.push(person);
console.log(list);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="addform">
<input type="text" name="fname" placeholder="First name">
<input type="text" name="lname" placeholder="Last name">
<input type="email" name="email" placeholder="email">
<input type="input" name="address" placeholder="address">
<input type="tel" name="phone" placeholder="phone number">
<button type="submit" id="submitbtn">Submit</button>
<button type="button" id="closebtn">Close</button>
</form>
'updateValue ($ (this)) .val());'는 $ ($ this)와 같습니다.) .val());'전혀 이해가되지 않습니다. 그것은 'this.value = this.value'를하고 있습니다. 아무것도 변경하지 않습니다. – Bergi
[제출할 준비가 된] 리스너를 추가 하시겠습니까 (https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid). -not-find-the-element)? 스택 스 니펫은 그렇지 않습니다. – Bergi
내가 틀릴 수도 있지만,이 종류의 선택기는 객체 대신 배열을 선택하려고 할 수 있습니다. 대신 배열의 val을 얻으려고하면 첫 번째 요소가됩니다. – Kejt