使用原型提交远程表单

时间:2010-06-06 07:35:22

标签: javascript ruby-on-rails prototypejs

我有一个这样的远程表单,里面有一个复选框。当我选择或取消选中我想要的复选框时

  1. 设置隐藏字段的值

  2. ajax将此表单提交到指定的网址。

  3. 我尝试了$('search_form')。onsubmit(),但是我收到一个错误,说onsubmit不是一个函数。使用原型。这是最好的方法吗?

    <form onsubmit="new Ajax.Request('/searches/search_set?stype=1', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" method="post" id="new_search" class="new_search" action="/searches/search_set?stype=1">
      <div style="margin: 0pt; padding: 0pt; display: inline;">
        <input type="hidden" value="3TWSyMsZXI0nltz7zHAxuj1KX=" name="authenticity_token">
      </div>
    
      <a onclick="setSubmit(this);" href="#" class="submit-link-button fg-button ui-state-default fg-button-icon-left ui-corner-all" id="search_submit">
        <span class="ui-icon ui-icon-search"></span>'Search'
      </a>
      </div>   
    
      <input type="checkbox" value="Energy" onclick="refreshResults(this);" name="search[conditions][article_tag][0]" id="search_conditions_article_tag_0">
    

1 个答案:

答案 0 :(得分:0)

也许你可以尝试这样的事情:

<form method="post" id="new_search" class="new_search" action="/searches/search_set?stype=1">
  <a href="#" class="submit-link-button fg-button ui-state-default fg-button-icon-left ui-corner-all" id="search_submit">
    <span class="ui-icon ui-icon-search">Search</span>
  </a>
  <input type="hidden" name="hidden_field_name" value="" id="hidden_field_id" />
  <input type="checkbox" value="Energy" name="search[conditions][article_tag][0]" id="search_conditions_article_tag_0" />
</form>

<script type="text/javascript">
  var Search = {
    send: function(event, element) {
      $('hidden_field_id').value = element.value;
      var form = element.up('form');
      new Ajax.Request('/searches/search_set?stype=1', {asynchronous:true, evalScripts:true, parameters:Form.serialize(form)}); return false;
      Event.stop(event);
    }
  };
  document.observe('dom:loaded', function(event) {
    $("search_conditions_article_tag_0").on("click", Search.send);
  });
</script>

这就是你要找的东西吗?