开发者

replace input button with jquery

开发者 https://www.devze.com 2023-02-23 14:05 出处:网络
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:

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
 });
0

精彩评论

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