【问题标题】:Function call before form submit表单提交前的函数调用
【发布时间】:2020-03-13 14:42:09
【问题描述】:

我试图在提交表单之前调用一个函数。 这是html

<form id="form_email" method="POST" name="form_email">
<h2>Quel est votre email ?</h2>
<hr>
    <div id="mail">
    {{form_email.mail}}
        </div>

<button type="button" name="Envoi" onclick="hide_generate();">Télécharger</button>
</form>

这里是js函数

function hide_generate(){
if ( document.getElementById('mail').value == "" ) {
alert("Fill All Fields !");
} else {
document.getElementById('abc').style.display = "none";
generatePDF();
document.getElementById('form_email').submit();
}
}


function generatePDF() {
    var element = document.getElementById('content');
    var opt = {
      margin:       1,
      filename:     'rapport_analyse.pdf',
      image:        { type: 'jpeg', quality: 0.98 },
      html2canvas:  { scale: 2  },
      jsPDF:        { unit: 'in', format: 'A4', orientation: 'portrait' }
    };

    // New Promise-based usage:
    html2pdf().from(element).set(opt).save();
    }

问题是表单直接提交,没有调用generatePDF()函数... 感谢您的帮助!

【问题讨论】:

  • 您为div 设置了一个ID mail。并尝试访问div 的值。 document.getElementById('mail').value注意 div 没有value。您需要使用innerHTMLinnerText
  • 附带说明,您应该处理提交事件,而不是单击按钮,因为用户可以通过按 Enter 提交表单。

标签: javascript html forms function


【解决方案1】:

您可以将函数附加到按钮单击事件。 使用 jQuery:

$("btn-envoi").on('click', hide_generate);

使用纯javascript:

var button = document.getElementById('btn-envoi');
button.addEventListener('click', hide_generate);

    // Validating Empty Field
    function check_empty() {
          if ( document.getElementById('email').value == "" ) {
                alert("Fill All Fields !");
          } else {
                document.getElementById('form').submit();
                alert("Form Submitted Successfully...");
          }
    
    }
    //Function To Display Popup
    function div_show() {
          document.getElementById('abc').style.display = "block";
    }
    //Function to Hide Popup
    function div_hide(){
          document.getElementById('abc').style.display = "none";
    }
    
    function hide_generate(ev){
        if (document.getElementById('email').value == "" ) {
            alert("Fill All Fields !");
        } else {
            document.getElementById('abc').style.display = "none";
            generatePDF();
            document.getElementById('form_email').submit();
        }
    }
    
    
    var button = document.getElementById('btn-envoi');
    button.addEventListener('click', hide_generate);
    <div id="abc">
    <!-- Popup Div Starts Here -->
    <div id="popupContact">
    <!-- Contact Us Form -->
      <form id="form_email" method="POST" name="form_email">
        <h2>Quel est votre email ?</h2>
        <hr>
          <input type="text" id="email" />
    
        <button type="button" name="Envoi" id="btn-envoi">Télécharger</button>
      </form>
    </div>
    <!-- Popup Div Ends Here -->
    </div>
    <!-- Display Popup Button -->
    <h1>Telécharger le rapport d'analyse</h1>
    <button id="popup" onclick="div_show()">Télécharger le rapport au format PDF</button>

【讨论】:

  • 按钮不会自动提交表单,它会显示 type="button"。这意味着 OP 也不需要调用 preventDefault()
  • 嗨 Bruno 和 Juan,谢谢你的回答,我试过了,但是当我点击按钮时没有任何反应,我把完整的代码放在下面
  • 再次感谢布鲁诺,我们几乎就在那里,但现在它提交表单而不编辑 pdf(由 generatePDF 函数完成),并且该函数工作正常,因为它会生成 PDF 如果我输入document.getElementById('form_email').submit();在评论中加入...
  • generatePDF 函数是异步的吗?如果是这样,可能有一个回调在 PDF 完成时执行,您需要将 submit() 函数调用移动到该回调内部。
  • 我在问题中添加了 generatePDF() 函数代码。我试图将提交放在函数的末尾,但随后直接提交而不生成 PDF。你是对的,这一定是异步的问题,但我不熟悉这个......
猜你喜欢
  • 2013-11-12
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多