I'm trying to replace a Submit button in a form with a javascript function. I'm able to insert the following link_to_function for su开发者_如何学编程bmitting the form:
<%= link_to_function 'Create', "$('form').submit()" %>
Now, I want to replace the default form submit button with the js function above with jquery to have a fallback option when js is disabled. The question is now:
how do I replace the ordinary submit button inside my application.js file?
I tried:
$('input:submit').replaceWith("<%= escape_javascript(link_to_function 'Create', "$('form').submit()") %>");
which doesn't seem to work :/
In my application I do something similar - I have a button which performs the submit action on any present form.
the JQuery:
$('#save').click(function(){
$('form').submit();
return false;
});
I would suggest that you keep your submit button then add a event to it then prevent the submit I'm assuming that you want to make an ajax call when you submit ?
the html
<input type="submit" class="submit" />
the jQuery
$('.submit').click(function(event){
event.preventDefault();
//do what you want here
});
精彩评论