Rails:每次点击带有按钮标记的元素时,都会提交ajax / remote表单

时间:2013-09-23 14:21:44

标签: ruby-on-rails ajax

我昨晚才注意到它,并且从那以后一直在努力弄清楚它为什么这样做。我有一个响应js的ajax / remote表单。当我点击任何带有按钮标签的元素时,我的表单会提交它。知道这里发生了什么吗?


一些代码:(我在使用jquery通过某些单词悬停时弹出按钮,但在添加它们之前存在问题)

<%= semantic_form_for(@post, :html => {:multipart => true, class: "form-vertical"}, :remote => true) do |f| %>
<%= f.text_field :name %>
<%= f.text_area :description %> <!-- a li list of 4 horizontal buttons popup when hover -->

<button class="btn-save"> <!-- found out I didn't even need the input submit button -->
    <span>Save</span>
</button>
<% end %>

#posts controller
# GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(post_params_update)
        flash.now[:notice] = "successfully updated!"
        format.html { redirect_to edit_post_path(@post), notice: 'successfully updated!' }
        format.json { render json: edit_post_path(@post), status: :created, location: @post }
        format.js 
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
        format.js
      end
    end
  end

我甚至尝试删除所有其余的js文件..仍然是相同的

1 个答案:

答案 0 :(得分:1)

如果您不希望在单击按钮时提交表单,则需要在按钮标记中添加type =“按钮。

<button type="button">...<button>
相关问题