如何根据所选字段动态更改表单中的URL或操作?

时间:2014-01-22 09:43:24

标签: javascript ruby-on-rails ruby ajax ransack

如何根据所选字段动态更改网址?

= 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

如果表单已在temp中选择了time_type_id_eq选项,我希望将此表单的网址或操作设置为temporary_offers_path,类似于permanent更改路径{{1} }}

permanent_offers_path

我使用Ransack作为搜索者。

2 个答案:

答案 0 :(得分:2)

这个coffescript示例可能很有用:

  $("#submit_search_form").click (e) ->
    search_type = $('select#search_type').val();
    if search_type == 'Client'
      $("#search_form").attr("action", "/orders/#{order_id}")

答案 1 :(得分:0)

在这种情况下,我将使用复选框。并使用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>

注意:这只是您理解的示例代码