[英]Rails/jQuery prevent form submission
jQUery
prevent form submission. jQUery
阻止表单提交。
$('#edit_user_form').submit(function(){
$("textarea[name = user_form_snippet]").enabled = enabled;
return false;
e.preventDefault();
});
Below is my rails form. 下面是我的表格。
<%form_for @user, :html {:id => "edit_user_form" },
:url => {action => "update", :id => @user.id} do |f| %>
<% end %>
My jQuery
function is getting called, but for strange reason the form is getting submitted even if use either return false
or e.preventDefault() 我的
jQuery
函数被调用,但是出于奇怪的原因,即使使用return false
或e.preventDefault(),也正在提交表单
If you want to use e.preventDefault()
, you must have it before your return
statement, as the return
is always the last thing that happens in a function. 如果要使用
e.preventDefault()
,则必须在return
语句之前使用它,因为return
始终是函数中发生的最后一件事。
You should also update your code to include the e
in the anonymous function, otherwise e.preventDefault()
doesn't know what e
is :) 您还应该更新代码以在匿名函数中包含
e
,否则e.preventDefault()
不知道e
是什么:)
http://jsfiddle.net/UEK7d/ <- Example fiddle http://jsfiddle.net/UEK7d/ <-小提琴示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.