Passing data from a form value to ajax call (Javascript)
This may seem like a nub question. I’ll be the first to admit my knowledge of JavaScript is limited, I’m very desperately trying to learn, though. I’m having a bit of trouble when attempting to pass data to an additional field.
$(function () {
$('#upload_file').submit(function (e) {
e.preventDefault();
$.ajaxFileUpload({
url: './upload/upload_file/',
secureuri: false,
fileElementId: 'userfile',
dataType: 'json',
data: {
'title': $('#title').val()
},
success: function (data, status) {
if (data.status != 'error') {
$('#files').html('<p>Reloading files...</p>');
refresh_files();
开发者_JS百科 $('#title').val('');
}
alert(data.msg);
}
});
return false;
});
});
As you can see above, I'm passing across the title value in the data parameter of the AJAX call. This is successful. The question is, how would I go about passin additional forum values within the data param?
Just separate the next values with commas.
data : {
title : $('#title').val(),
name : "hi",
message : "I'm another param"
},
An easy way to grab all form values is like this:
data: $('#my-form').serialize()
Works pretty well for me in most cases, unless I only want to grab specific values. You can read more about it here: http://api.jquery.com/serialize/
Either way, the above answer is probably what you want, I just thought I'd throw in a quick tip. I was pretty pumped when I first figured this out :)
精彩评论