문제

I have a rails form with a submit button, but I'm wondering if it's possible to make this submit button 1) Post to controller create action and also 2) Execute Javascript

Here are the two buttons I essentially want to combine:

1. <%= f.submit "Go!", class: "btn btn-primary btn-large btn-block" %>

2. <a href="javascript:ExtInstall()" class="btn btn-default btn-large btn-block" role="button">Download our Google Chrome extension</a>
도움이 되었습니까?

해결책 2

If your button is part of a form, you can keep its functionality the same as it is now. If it's stand-alone, you'll be best using button_to like so:

<%= button_to "Download", post_path, id: "button" %> # -> uses POST http verb, so will send to create action

What you're asking is best answered with unobtrusive javascript. This will bind your button's click event to a function of your choice, allowing you to not only submit the required data, but handle the other functionality you need too:

#app/assets/javascripts/application.js
$("#button").on("click", function(){
    ExtInstall(); //do you have any references for this function?
});

다른 팁

Yes, you can do this by adding an onClick handler in JS to the submit button.

You can see submit_tag with javascript function for the different ways you can do this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top