开发者

Use jQuery to fire the click event on all a Div's Submit buttons

I have a form with multiple submit buttons. This bit of jQuery code runs before the form is transmitted:

$(":submit").click(function () {
        $("input[name='social']").val($(this).prevAll(".social").html());
        $("input[name='footer']").val($(this).prevAll(".footer").text());
});

So one form, multiple buttons that, when clicked initialize the form's values to carry values found inside the given DIV (those elements that are sibling to the given submit button).

That much works - clicking the buttons indivdually. Now I'd like to implement a 'Click All' button that fires each of the submit buttons in turn. Here's the logic i'm pursuring:

    $("#submitAll").click(function () {
        $("#allPerDay input:submit"开发者_开发百科).each(function () {
            //$(this).trigger('click');
            alert("hit " + this.value);
        });
    });

As is, that code will alert all the submit buttons, but when I try to add the .trigger function, only the first button fires. I suspect it's a matter of scope - while the form is correctly populated and submitted (abet just once), other aspects of response handling are hosed.

Is there an approach that would create a pause until some sort of success response is heard?

FOLLOWUP: The structure is:

<form ....>
    <div id=sibling1>
        <div class=social>values to insert to form</div>
        <div class=footer>other values to insert</div>
        <input type="submit" value="Name of First Group" />
    </div>
        <div id=sibling2>
        <div class=social>another set of values to insert to form</div>
        <div class=footer>another set of other values to insert</div>
        <input type="submit" value="Name of 2nd Group" />
    </div>
</form>

And perhaps more importantly, I'm using this code to handle the server's reply. That means this is an AJAX-submitted form. To repeat, the original form (user clicks each DIV's submit button) works and honors the 'success' handler. Now that I'm trying to 'Submit All', the form submits (just once) but the AJAX handling is never run.

    var options = {
        target: '#output'   
    , success: showResponse  
    };

    $('#frmPerDay').ajaxForm(options);


Use:

 $("#allPerDay input:submit").click();

It's simple as that :)

It will fire a click() event on all of the buttons

Also, you cant use any function that you would use on $(this) on other selectors like ("#allPerDay input:submit")(meaning the method would be applied to all of the selected elements), because essentially they are the same type of object (jQuery object) and have the same methods.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜