make objects and fields dynamically in javascript
instead of using getParameterByName('Field', PostData) (PostData == $('form').seri开发者_C百科alize();)
I would like to write PostData.Field, how can i do that with javascript?
You could write your own extension to return an object like you want, here's what that looks like:
jQuery.fn.MakeIntoFields = function() {
var arr = this.serializeArray();
var props = {};
$.each(arr, function(i, f) {
props[f.name] = f.value;
});
return props;
};
You'd call it by doing this:
var PostData = $("form").MakeIntoFields();
Then you could access the values with dot notation like you want:
PostData.fieldNameHere
//or...
PostData["fieldNameHere"]
You can see this working against a demo <form>
here
Do you mean something like this?
PostData = {
field: $('input[name=Field]').val(),
otherData: 'customdata'
};
精彩评论