从按钮标签提交表单 [英] Submit form from a button tag

查看:146
本文介绍了从按钮标签提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在提交表单时添加了一个事件侦听器,这是代码:

I added an event listener when my form is submitted, this is the code:

var formo = document.getElementById("ing");
formo.addEventListener("submit", validation, false);

但我正在使用带有此代码的按钮代码提交表单:

but I'm submitting the form with a button tag with this code:

var enviar = document.getElementById("submit_btn");
enviar.addEventListener("click", envioFormulario, false);

function envioFormulario() {
  this.disabled = true;
  this.value = "Sending";
  this.form.submit();
}

与此表单提交但提交事件(第一行代码)似乎没有办法使我能做到这一点。

with this the form is submitted but the submit event (the first lines of code) doesn't seems to work what can I do to make it work?

推荐答案

我同意@Mathletics的评论。只要你点击确认,并提交,如果它通过验证:

I agree with @Mathletics comment. Just do the validation when you click, and submit if it passes validation:

var enviar = document.getElementById("submit_btn");
enviar.addEventListener("click", envioFormulario, false);

function envioFormulario() {
  if (validation()) {
      this.disabled = true;
      this.value = "Sending";
      this.form.submit();
  } else {
      alert("Validation failed. Didn't submit");
  }
}

这篇关于从按钮标签提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆