开发者

ajax link inside an ajax form calls form submission for no reason rails 3

I have a form of following structure

ROR code"

form_tag class='remo开发者_StackOverflow中文版te_form' remote=>true
{
some fields
1
2
3
one <a> link remote=>true
4
submit button
}

jquery code

$('.remote_form').live('ajax:beforeSend', function(event, elements){
do some blah blah
}

now when I click "one link" "blah blah" thing executes . why is that so ? basically only when I click submit "blah blah" must get executed.

Have I done anything wrong ?


My idea : The form contains a few elements . and before the form gets submitted I need to do a few things so I use ajax:before send for remote_form . Now I also have some link which is an ajax request totally independent of form. Obviously the one link shouldn't cause the execution of "blah blah "..


ajax:beforeSend is meant for all ajax calls. That's why the function was getting executed. if you want the function to be executed only on form submit, bind that function to form's submit event like

$('#myForm2').submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options); 

        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    });

But jquery has a plugin which can do all these things for you. I think you should have a look at the jquery form plugin which explains everything about submitting a form using ajax.


I'm not sure to understand. Your problem is that the callback is fired although the form hasn't been submitted ?

Did you try with the classic $.ajax() function :

$('.remote_form').submit(function(){
    $.ajax({
        beforeSend: function(){
            // do something
        }
    });
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜