【发布时间】:2014-06-05 13:00:17
【问题描述】:
我在提交表单时遇到问题,它正在创建一个重复的 post 方法,我不知道为什么,因为我在我的 js 中阻止了 Default。房间里有灯饰吗?
我的 JS
<% if @photo.user = current_user %>
<script> $("#tp_tag_save").on('click', function(event) {
event.preventDefault();
$.post("/users/<%= current_user.id %>/photos/<%= @photo.id %>/tags", {
"tag[name]" : $("#tag_name").val(),
"tag[location]" : $("#tag_location").val(),
"tag[price]" : $("#tag_price").val(),
"tag[coordinate_x]" : $("#tag_coordinate_x").val(),
"tag[coordinate_y]" : $("#tag_coordinate_y").val()
}).done(function (data) {
drawTag($("#tag_name").val(), $("#tag_price").val(), $("#tag_location").val(), $("#tag_coordinate_x").val(), $("#tag_coordinate_y").val()),
reDrawList();
});
modal("#tag_modal", "hide", 200);
});
</script>
<% end %>
我的表单
<div class="large-5 center semi_padding_top columns large-centered ">
<h1> <i class="icon-photo"></i>Subir un spot</h1>
<h6 class="lightgrey thin">Los spots son rincones de tu casa, oficina o negocio de los que estés orgulloso de cómo están decorados. </h6>
<%= simple_form_for [current_user, @photo] , html_input: "data-abide" do |f| %>
<div class="new_spot_form deco-form semi_padding_top">
<%= f.input :title , label: "Ponle un título a tu spot", html_input: "required", class: "input" %>
<%= f.input :description, label: "¿Qué te gusta de este spot?" , class: "textarea" %>
<%= f.input :photo, label: "Ahora sube tu foto", input_html: { id: 'fileElem'} %>
<button id="fileSelect" class="semi_margin_bottom" type="button"><i class="valign-m quarter_padding_right icon-magnifier"></i>Seleccionar spot</button>
<%= f.label "Selecciona el estilo de tu spot" %>
<%= f.collection_select :category_id, Category.all, :id, :name%>
<%= f.label "Selecciona la zona de tu spot" %>
<%= f.collection_select :zone_id, Zone.all, :id, :name %>
<%= f.input :terms , label: "Tengo los derechos de esta foto" %>
<%= f.button :submit, "Subir Spot", input_html: {class: 'icon-magnifier button'} %>
</div>
<% end %>
</div>
【问题讨论】:
-
控制台有错误吗?
-
@Pete 不,控制台中没有错误...这真的很奇怪...
-
最常见的原因是事件被绑定了两次。
$("#tp_tag_save").on('click', function(event) {})具体来说。如果该行代码出于任何原因运行两次,则使用该 ID 的 DOM 元素将触发两次。确保只绑定一次事件 -
@Patrick 我知道,但问题是我看不到我在哪里绑定了两次!
-
@Patrick 找到了!我在另一个带有该#id 的 js 中有一个 switch 方法,我只是在 sublime 中搜索了我的所有文件。
标签: javascript jquery ruby-on-rails simple-form