我从下拉菜单中获得的两个值制作自定义网址时遇到了一点问题。我正在尝试使用jquery。这两个值的 id 为
#convertor_from_currency
#convertor_to_currency
jQuery:
<script>
$(document).ready(function(){
$("#currency_form").submit(function(e){
e.preventDefault();
var from = $('#convertor_from_currency').val();
var to = $('#convertor_to_currency').val();
var url_end = from + "-" + to;
var url = $('#currency_form').attr('action') + '/' + url_end;
$('#currency_form').attr('action', url);
$('#currency_form').submit();
//alert(url);
});
</script>
这是表单的标记:
<%= form_for :convertor , :html => {:id => "currency_form"}, :url => {:controller => "converter", :action => "show" } do |f| %>
<%= f.label :from_currency %>
<%= f.select :from_currency, options_for_select(currencies, @from_cur) %>
<%= f.label :to_currency %>
<%= f.select :to_currency, options_for_select(currencies, @to_cur), :required => true %>
<%= f.submit "Calculate", class: "btn btn-large btn-primary", id: "submitButton" %>
<% end %>
路线
root 'converter#index'
# get "converter/show"
match '/currencies/', to: 'converter#show', via: [:post]
在提交时,我希望网址为:
'/currencies/url_end
其中“url_end”是表单中提交的值的串联:
#convertor_from_currency
#convertor_to_currency
更新了,这是我的控制器。
class ConverterController < ApplicationController
def index
end
def show
@amount = params[:convertor][:amount]
@from_cur = params[:convertor][:from_currency]
@to_cur = params[:convertor][:to_currency]
@result = ConverterHelper.show(@from_cur, @to_cur, @amount)
end
end
我该怎么做呢?任何想法都会受到赞赏,即使它使用 jquery/javascript 以外的其他方式