I am dynamically creating forms from a JSON object that I fetch with a getJSON. If there is a better way of building forms in this manner, please let me know. I used this example to understand how to rebind submit button:
https://stackoverflow.com/questions/2754961/rebind-dymanically-created-forms-after-jquery-ajax-response**
What I need to know is how to grab the class of the input submit button I have clicked. There's an "Approve" and "Deny" button and depending on which one is clicked will be sent to the server in a POST so I know what to do with the form data being POST'ed on the server side.
Here's the code:
Here is what I need to change to be correct:
// this is not the value of the class attribute from submit button clicked.
var action = this.attr('class');
alert(action);
So I can get the class attribute from the button that was clicked.
UPDATE (8.27.2010) - Here's what I did for anyone who stumbles across this and needs an answer:
For the serializeObject() function, see this post: https://stackoverflow.com/questions/1184624/serialize-form-to-json-with-jquery
$(".formApprove").live('click', function() {
var fid = this.id.replace(/approve-/i, "");
var formObject = $('form#message-'+fid).serializeObject();
$.post('/path/to/ajaxhandler.php', {
a: 'setApproved',
id: fid,
subject: formObject.subject,
message: formObject.message
}, function(res) {
if(res.result == 0) {
alert ('done');
} else {
alert ('Error');
},"json");
return false;
});
It's hard to see what's going on without the markup being written, but I'd be careful about binding click events to submit buttons - remember that forms, by default, can be submitted when a user hits the Enter key.
以上就是How to get attritubes of form input when generating a form dynamically and using .live()的详细内容,更多请关注web前端其它相关文章!