Javascript 如何根据所选字段动态更改表单中的url或操作?
如何根据所选字段动态更改urlJavascript 如何根据所选字段动态更改表单中的url或操作?,javascript,ruby-on-rails,ruby,ajax,ransack,Javascript,Ruby On Rails,Ruby,Ajax,Ransack,如何根据所选字段动态更改url = search_form_for @q, url: offers_path, builder: SimpleForm::FormBuilder do |f| = f.input :name_cont = f.input :time_type_id_eq, collection: [['Promotion', 'temp'], ['Permanent', 'permanent']], include_blank: false = f.
= search_form_for @q, url: offers_path, builder: SimpleForm::FormBuilder do |f|
= f.input :name_cont
= f.input :time_type_id_eq,
collection: [['Promotion', 'temp'], ['Permanent', 'permanent']],
include_blank: false
= f.submit
如果表单将在time\u type\u id\u eq
中选择temp
选项,我想将此表单的url或操作设置为temporary\u offers\u path
类似地设置为permanent
将路径更改为permanent\u offers\u path
class OffersController < ApplicationController
def index
@temporary_offers = Offer.temporary
@permanent_offers = Offer.permanent
@recommended_offers = Offer.recommended
end
def temporary
@q = Offer.search(params[:q])
@temporary_offers = @q.result(distinct: true).temporary
end
def permanent
@q = Offer.search(params[:q])
@permanent_offers = @q.result(distinct: true).permanent
end
end
class-OffersController
我使用Ransack作为搜索者。这个coffescript示例可能很有用:
$("#submit_search_form").click (e) ->
search_type = $('select#search_type').val();
if search_type == 'Client'
$("#search_form").attr("action", "/orders/#{order_id}")
在本例中,我将使用复选框,并使用JavaScript和HTML 首先假设您的表单名为myform,并使用HTML表单
<form name="myform" onsubmit="return OnSubmitForm();">
<input type="radio" name="Promotion" value="1">Temporary
<input type="radio" name="Promotion" value="2">Permanent
<p>
<input type="submit" name="submit" value="save">
</p>
</form>
暂时的
永久的
JavaScript:
<script type="text/javascript">
function OnSubmitForm()
{
if(document.myform.Promotion[0].checked == true)
{
document.myform.action ="temporary.html";
}
else
if(document.myform.Promotion[1].checked == true)
{
document.myform.action ="permanent.html";
}
return true;
}
</script>
子表单()上的函数
{
if(document.myform.Promotion[0]。选中==true)
{
document.myform.action=“temporary.html”;
}
其他的
if(document.myform.Promotion[1]。选中==true)
{
document.myform.action=“permanent.html”;
}
返回true;
}
注意:这只是一个供您理解的示例代码您想直接执行正确的操作吗?或者你关注的是URL的不同吗?我在两个控制器视图中为每个操作呈现此表单“提供\u控制器”和“合作伙伴\u控制器”,但我只想路由到正确的操作我想也许我可以在ruby端处理它