简体   繁体   English

如果没有任何操作,是否需要使用HTML表单标签?

[英]Do I need to use HTML form tag if there is no action?

I am creating a registration form, where the user needs to enter an email and a password. 我正在创建一个注册表,用户需要在其中输入电子邮件和密码。 I know that in general this kind of forms are wrapped in an html form tag which usually has action attribute which sends the information from the inputs to the server and stuff like that. 我知道,通常将这种形式包装在html形式标签中,该标签通常具有action属性,该属性将信息从输入发送到服务器以及类似的东西。 But in my case, I don't need to use any action attribute, because I simply take the information from email and password inputs and do my stuff with those values, such as checking if the email and/or the password are valid and proper, etc and then I send this info to Firebase realtime database. 但就我而言,我不需要使用任何action属性,因为我只是从电子邮件和密码输入中获取信息,并使用这些值来处理我的事情,例如检查电子邮件和/或密码是否有效和正确等等,然后将此信息发送到Firebase实时数据库。 So, I use only Javascript/jQuery to grab the information I need and send it wherever I need which completely works for me. 因此,我仅使用Javascript / jQuery来获取所需的信息,然后将其发送到我需要的任何地方,而这些信息对我完全有效。

My question is do I still need to put these inputs inside a form tag and not specify action attribute or specify an action attribute with the value of empty string or I can just put them in a div and do my stuff in a way I am doing right now. 我的问题是我是否仍然需要将这些输入放在表单标签中,而不是指定action属性或使用空字符串值指定action属性,还是可以将它们放在div中并以我正在做的方式来做马上。 Is there anything wrong in the way I am doing this? 我这样做有什么问题吗?

I read a few articles about html form tag and action attribute, but none of them answered to this specific question. 我读了几篇有关html表单标签和action属性的文章,但是都没有回答这个特定问题。

I think no code is needed 我认为不需要代码

Is there anything wrong in the way I am doing this? 我这样做有什么问题吗?

besides potential accessibility hickups, less semantic markup, and no functionality for people who turn off JavaScript, not really =)... 除了潜在的可访问性组合,更少的语义标记以及关闭JavaScript的人没有功能外,不是真的=)...

depending on how you handle the inputs (eg on('change', ...) ) and submitting the data to your backend for checking the user input you might benefit from having the from tag present because with it you can 根据您处理输入的方式(例如on('change', ...) )并将数据提交到后端以检查用户输入,您可以从显示from标记中受益,因为有了它,您可以

a) listen for submit events and b) serialize the entire form once it is submitted instead of having to collect each individual input a)监听submit事件,b)提交表单后序列化整个表单,而不必收集每个单独的输入

but that completely depends on your implementation. 但这完全取决于您的实现。

不,如果没有要执行的操作,则无需将input form

If you use javascript to submit data the form tag isn't mandatory , but if include it not cause you problem maybe you can find it useful for some reason: 如果您使用javascript提交数据,则form标记不是强制性的 ,但如果包含它不会引起问题,则由于某些原因,您可能会发现它有用:

  • if you want to attach ajax to submit action 如果您想附加ajax来提交操作
var form = document.querySelector(',myform');

form.addEventListener('submit', function () {
  console.log('submitted');
  });
  • or if you want to use some ready to use js validation library 或者如果您想使用一些现成的js验证库
  • to serialize data 序列化数据
  • ... ...

Use it if it isn't a problem, maybe you'll need it in the future. 如果没有问题,请使用它,也许将来您会需要它。

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

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