当前位置: 动力学知识库 > 问答 > 编程问答 >

Set a javascript object's attribute with jQuery.

问题描述:

How do I correctly set a javascript object's attribute? My console.log() shouldn't return undefined.

First I declare a class:

function SignUpForm(myFormElement) {

var eForm, email, password;

this.eForm = myFormElement;

}

Then on page load, I instantiate the class:

$(document).ready(function(){

myFormElement = $('form');

myForm = new SignUpForm(myFormElement);

console.log(myForm.eForm); // Returns undefined :(

});

网友答案:
function SignUpForm(myFormElement) {
  this.eForm = myFormElement;
}

you can safely remove your declared scope variables as they don't do anything.

网友答案:

You maybe want something like this?!

HTML:

<form>
    <input name="email" />
    <input name="password" />
</form>

Javascript:

Here you define SignUpForm class and in eForm you serialize form:

function SignUpForm(myFormElement) {
    this.eForm = myFormElement.serialize();
}

$(document).ready(function(){
    myFormElement = $('form');
    myForm = new SignUpForm(myFormElement);
    console.log(myForm.eForm);
});

jsFiddle

EDIT: If eForm store element, then your code is ok ... but console.log(myForm.eForm); doesnt make much sense, and for rest you can serialize that form element console.log(myForm.eForm.serialize());.

网友答案:

I see nothing wrong with this currently. I don't use log much, but am not sure if you can "log" an object, or if it has to be a type string. Other than that, the object is being set properly. myForm is an object, eForm is a property of the object with it's value being the jquery object of the selector "FORM".

分享给朋友:
您可能感兴趣的文章:
随机阅读: