【问题标题】:submit the form but page does not reload. preventDefault() not working提交表单但页面不会重新加载。防止默认()不工作
【发布时间】:2020-08-22 11:09:27
【问题描述】:

当我使用表单并尝试提交时,它会重新加载页面。但我的要求是提交表单但不重新加载页面并发送数据提及此操作url。 我知道这个重复的问题
Form

    <form id="leadsquareForm" name="leadsquareForm" class="leadsquare-form-single-course" method="post" enctype="multipart/form-data" action="https://myUrl.jsp">
                  <h3 class="talk-to-expert" style="width: fit-content;width: -moz-fit-content; margin: 0 auto 15px;">Wish to talk to an expert?</h3>
                  <div style=" margin: 0 auto 15px; width: -moz-fit-content; width: fit-content;">
                    <a href="tel:+919599000000"><i class="fa fa-phone" style=" padding: 5px 10px; background: #25D366; color: #fff; border-radius: 7px; font-size: 30px; margin-right: 8px;"></i></a>
                    <div style="float: right; font-size: 15px; line-height: 1.3; font-weight: 600;">
                      <div style="color: #00d56b;"><a href="tel:+919599000000">+91 95990-00000</a></div>
                      <div style="color: #f2aa3c;">Call Now for Expert Counselling</div>
                    </div>
                    <!--&lt;!&ndash;<h3 style="width: fit-content; width: -moz-fit-content; margin: 0 auto">OR</h3>&ndash;&gt;-->
                  </div>
                  <div class="mdl-textfield mdl-js-textfield">
                    <input class="mdl-textfield__input" type='text' id='FirstName' name='FirstName' placeholder="Name *" required='required' />
                    <label class="mdl-textfield__label" for='FirstName'>Name *</label>
                  </div>
    
                  <div class="mdl-textfield mdl-js-textfield">
                    <input class="mdl-textfield__input" type='email' id='EmailAddress' name='EmailAddress' placeholder="Email *" required='required' />
                    <label class="mdl-textfield__label" for='EmailAddress'>Email *</label>
                  </div>
    
                  <div class="mdl-textfield mdl-js-textfield">
                    <input class="mdl-textfield__input" type='tel' id='Phone' name='Phone'  placeholder="Phone Number *" required='required' onkeypress="if(this.value.length==10){return false;}" />
                    <label class="mdl-textfield__label" for='Phone'>Phone Number *</label>
                  </div>

提交

不行,我已经加入了这个方法。

$("#leadsquareForm").submit(function(event){
    alert('intercept');
    event.preventDefault();
    console.log(event);
});

【问题讨论】:

  • 您是如何创建表单的?它是在页面加载后生成的(例如在 ajax 调用之后)?
  • 是否有任何控制台错误?你收到第一个alert了吗? (对现有答案的评论表明不是) - 如果是这种情况(没有警报),那么您的事件没有触发,所以 event.preventDefault() 甚至没有运行,所以它不是“不工作”。
  • 是的,重新加载页面后创建表单
  • 这能回答你的问题吗? Event binding on dynamically created elements?
  • 将您的 $("#leadsquareForm").submit(function(... 更改为 $(document).on("submit", "#leadsquareForm", function(... - 您需要事件委托

标签: javascript html jquery forms preventdefault


【解决方案1】:

preventDefault() 在这种情况下会阻止您提交表单。如果您尝试运行一些代码然后提交表单,那么您可以尝试以下操作:

   $("#leadsquareForm").submit(function(event){
       alert('intercept');
       event.preventDefault();
       console.log(event);
       // Run some code...

       // Then submit the form
       $("#leadsquareForm").submit()
   });

【讨论】:

  • 先尝试event.preventDefault(),然后再尝试alert('intercept')..
  • 如果没有alert,那么事件处理程序首先不会被调用。 event.preventDefault() 的顺序无关紧要 - 它可以是第一个或最后一个 - 如果是最后一个,那么如果在它之前还有另一个未处理的脚本错误,它可能会失败 - 但在这种情况下,它根本不会被调用
  • 检查你的控制台,可能有一些语法错误,似乎没有调用事件处理程序。
【解决方案2】:

$('#leadsquareForm').one('submit', function myFormSubmitCallback(event){
  alert('intercept');
  event.stopPropagation();
  event.preventDefault();
  var $this = $(this);
  $this.one('submit', myFormSubmitCallback);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <form id="leadsquareForm" name="leadsquareForm" class="leadsquare-form-single-course" method="post" enctype="multipart/form-data" action="https://myUrl.jsp">
    <h3 class="talk-to-expert" style="width: fit-content;width: -moz-fit-content; margin: 0 auto 15px;">Wish to talk to an expert?</h3>
    <div style=" margin: 0 auto 15px; width: -moz-fit-content; width: fit-content;">
      <a href="tel:+919599000000"><i class="fa fa-phone" style=" padding: 5px 10px; background: #25D366; color: #fff; border-radius: 7px; font-size: 30px; margin-right: 8px;"></i></a>
      <div style="float: right; font-size: 15px; line-height: 1.3; font-weight: 600;">
        <div style="color: #00d56b;"><a href="tel:+919599000000">+91 95990-00000</a></div>
        <div style="color: #f2aa3c;">Call Now for Expert Counselling</div>
      </div>
      <!--&lt;!&ndash;<h3 style="width: fit-content; width: -moz-fit-content; margin: 0 auto">OR</h3>&ndash;&gt;-->
    </div>
    <div class="mdl-textfield mdl-js-textfield">
      <input class="mdl-textfield__input" type='text' id='FirstName' name='FirstName' placeholder="Name *" required='required' />
      <label class="mdl-textfield__label" for='FirstName'>Name *</label>
    </div>

    <div class="mdl-textfield mdl-js-textfield">
      <input class="mdl-textfield__input" type='email' id='EmailAddress' name='EmailAddress' placeholder="Email *" required='required' />
      <label class="mdl-textfield__label" for='EmailAddress'>Email *</label>
    </div>

    <div class="mdl-textfield mdl-js-textfield">
      <input class="mdl-textfield__input" type='tel' id='Phone' name='Phone'  placeholder="Phone Number *" required='required' onkeypress="if(this.value.length==10){return false;}" />
      <label class="mdl-textfield__label" for='Phone'>Phone Number *</label>
    </div>
    <button class="mdl-button"  style="background: rgba(158, 158, 158, .2);" id="form-submit-button" type="submit">Submit</button>
  </form>

【讨论】:

  • 现在检查兄弟:)
  • form.submit 不执行。我不知道...?
  • 对不起,现在试试你没有通过表单因为没有得到事件。
  • 如果有什么问题评论我...!
  • 你能用这个sn-p检查一下吗?这工作正常。
猜你喜欢
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 2015-07-04
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多