简体   繁体   English

禁用表单默认行为

[英]Disabling form default behavior rails

I am trying to disable my default form behavior. 我正在尝试禁用默认表单行为。 I know typically to do this I am supposed to return false but that is not working. 我知道通常这样做,我应该返回false,但这是行不通的。 Does disabling default behavior only work after making a post request? 禁用默认行为仅在发出发布请求后才起作用吗? I want to disable it because 我想禁用它,因为

A) I want to see if my values are being serialized correctly. A)我想查看我的值是否正确序列化。

B) I want to make a server request in the future without using post, but rather by using websockets B)我想在将来不使用post而是通过使用websockets来发出服务器请求

Current code: 当前代码:

$(document).ready(function() {
  $(".attack").submit(function() {
    var values = $(this).serialize();
    console.log(values);
    return false;
  });
  return false;
});

Are you sure the CSS selector ".attack" applies to the form? 您确定CSS选择器“ .attack”适用于表单吗? Returning false is the right way of disabling the default behavior in this case, as you can see here . 返回false是禁止在这种情况下,默认行为,以正确的方式,你可以在这里看到

Maybe ".attack" applies to your button? 也许“ .attack”适用于您的按钮? If that's the case, you can use $(".attack").click instead (but you still have to return false or receive the event as a parameter and call event.preventDefault() before exiting the function). 在这种情况下,您可以使用$(".attack").click代替(但是您仍然必须返回false或将事件作为参数接收,并在退出函数之前调用event.preventDefault() )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM