开发者

How to get attritubes of form input when generating a form dynamically and using .live()

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 ho开发者_开发知识库w to rebind submit button: 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:

<script type="text/javascript">
$().ready(function(){
    jQuery("form[id^='message-']").live('submit', function(){

        var id = parseInt(this.id.replace("message-", ""));

        // this is not the value of the class attribute from submit button clicked.      
        var action = this.attr('class');
        alert(action);

        return false;

        jQuery.ajax({
            type: "POST",
            url: "/path/to/ajaxhandler.php",
            data: {"action": action, "id": id},
            success: function(response) {                
        }           
    });

    /*
        generate form
    */
    $.fn.getAllUnapprovedMessages = function () {
        $.getJSON("/messages.php?a=getAllUnapproved, function(data){
            var i=0;
            $("div#messages").empty();

            while(i<data.length) {
                $('div#messages').append(
                    '<div id="msg-'+data[i].id+'" style="border: 1px coral solid; margin: 5px 5px 5px 5px;">'+
                    '<form id="message-'+data[i].id+'" name="form-'+data[i].id+'" action="/path/to/ajaxhandler.php" >'+
                    'From: '+data[i].u_from+' ----> To: '+data[i].u_to+'<br />'+
                    'Subject: <br /><input name="subject" type="text" value="'+data[i].subject+'" /> <br /><br />'+
                    'Message: <br /><textarea rows="10" cols="60">'+data[i].text+'</textarea><br />'+
                    '<input class="formApprove" type="submit" id="approve-'+data[i].id+'" value="Approve" /> or '+
                    '<input class="formDeny" type="submit" id="deny-'+data[i].id+'" value="Deny" />'+
                    '</form></div>');
                i++;
            }
        });
    }
}
</script>

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: Convert form data to JavaScript object 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;
});


Short answer is, you can't. You can't get a reference to the element that caused the form to submit from the form's submit event alone. You'll need to wire up handlers to the buttons' click events too. If you give the input tags a name attribute, their value will be sent to the server with the rest of the form values during a normal, full post, but that doesn't help much when you want to do it via Ajax.

I'd suggest you modify the code to handle the buttons' click events and in that handler harvest the class/value attribute, then trigger the form's submit event and pass the attribute value in as custom event data, which you can then read from the form submit handler. If the passed data is null/undefined.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜