简体   繁体   English

如何防止页面跳到底部?

[英]How can i prevent page from jumping to bottom?

The page is jumping to the bottom when the submit button is pushed on the contact form. 当按下联系表单上的“提交”按钮时,页面跳至底部。 I think this is caused by js. 我认为这是由js引起的。 When you want to try it, push the link below, next contact, fill in the form and press "verstuur" 如果您想尝试,请点击下面的链接,下一个联系人,填写表格,然后按“ verstuur”

http://expertlemmer.nl/PC-laptop-reparatie-Lemmer/ http://expertlemmer.nl/PC-laptop-reparatie-Lemmer/

javascript: javascript:

$('form#contactForm button.submit').on('click', function() {

      $('#image-loader').fadeIn();

      var contactFname = $('#contactForm #contactFname').val();
      var contactLname = $('#contactForm #contactLname').val();
      var contactStraat = $('#contactForm #contactStraat').val();
      var contactPostcode = $('#contactForm #contactPostcode').val();
      var contactTel = $('#contactForm #contactTel').val();
      var contactEmail = $('#contactForm #contactEmail').val();
      var contactSubject = $('#contactForm #contactSubject').val();
      var contactMessage = $('#contactForm #contactMessage').val();

      var data = 'contactFname=' + contactFname  + '&contactLname=' + contactLname +
                 '&contactEmail=' + contactEmail + '&contactSubject=' + contactSubject +
                 '&contactStraat=' + contactStraat  + '&contactPostcode=' + contactPostcode  +
                 '&contactTel=' + contactTel  + '&contactMessage=' + contactMessage;

      $.ajax({

        type: "POST",
        url: "inc/sendEmail.php",
        data: data,
        success: function(msg) {

            // Message was sent
            if (msg == 'OK') {
               $('#image-loader').fadeOut();
               $('#message-warning').hide();
               $('#contactForm').fadeOut();
               $('#message-success').fadeIn();
            }
            // There was an error
            else {
               $('#image-loader').fadeOut();
               $('#message-warning').html(msg);
              $('#message-warning').fadeIn();
            }

        }

      });
      return false;
});

Hello i was on your webpage. 您好,我在您的网页上。 And you are using in your script : 您正在脚本中使用:

$('form#contactForm button.submit').on('click', function() {
   e.preventDefault();
// and so on

The problem is that, you do not pass the e in the function. 问题是,您没有在函数中传递e。 You have to use it like this : 您必须像这样使用它:

$('form#contactForm button.submit').on('click', function(e) {
       e.preventDefault();
    // and so on

Then it will prevent the form from refreshing the page. 然后,它将阻止表单刷新页面。 I am just guessing that you want to send the form through ajax. 我只是在猜测您要通过ajax发送表单。

Your form has no "ACTION" attribute, this means it's posting to the current URL. 您的表单没有“ ACTION”属性,这意味着它正在发布到当前URL。 The current URL has an anchor : #contact, this means you'll jump straight to the contact part. 当前网址的锚点为:#contact,这意味着您将直接跳至联系人部分。

See what you get when you go into this link and scroll down(Scroll, don't click contact), you'll jump to the review section. 看到您进入此链接并向下滚动时所得到的(滚动,不要单击联系人),您将跳到评论部分。

http://expertlemmer.nl/PC-laptop-reparatie-Lemmer/#journal http://expertlemmer.nl/PC-laptop-reparatie-Lemmer/#journal

Long story short, Put an action tag on your form :) 长话短说,在表单上放置一个动作标签:)

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

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