简体   繁体   English

防止表单提交与js禁用作出反应

[英]Prevent form submission in react with js disabled

I have a form in React that I want to be unsubmittable during the following 2 conditions: 我在React中有一个表单,我希望在以下两个条件下不能提交:

  1. JS is disabled JS已被禁用
  2. My App bundle hasn't been downloaded / parsed yet 我的应用程序包尚未下载/解析

The app is server side rendered, so the form is visible before the app bundle has finished downloading on the client side. 该应用程序是服务器端呈现的,因此在应用程序包在客户端完成下载之前,该表单是可见的。 If the form is submitted before the bundle finishes, the form will make a request and append the form parameters to the url. 如果在包完成之前提交表单,表单将发出请求并将表单参数附加到URL。

I've tried setting blank settings like 我尝试过设置空白设置

<form onSubmit="return false">
</form>

Setting the form to disabled initially and then enabling it on componentDidMount() seems like a solution, but I'm wondering if there's some HTML attribute I can add to the form or submit button instead. 最初将表单设置为禁用然后在componentDidMount()上启用它似乎是一个解决方案,但我想知道是否有一些HTML属性我可以添加到表单或提交按钮。

Disable your submit button and enable at the end of your JavaScript module. 禁用您的提交按钮并在JavaScript模块的末尾启用。

This will leave the button disabled if JavaScript is disabled, and is only activated after the JavaScript module has been executed. 如果禁用JavaScript,则会禁用该按钮,并且仅在执行JavaScript模块后激活该按钮。

 // In your JS module document.getElementById('submit').disabled = false 
 <form> <input id='submit' type='submit' disabled /> </form> 

Keep in mind though that this should in no way be used as a security measure . 请记住, 这绝不应该用作安全措施 Code on the client is inherently under the user's control and can be manipulated by the user without any restrictions. 客户端上的代码本身就在用户的控制之下,并且可以由用户操纵而没有任何限制。

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

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