简体   繁体   English

提交表单后,如何防止页面跳动?

[英]How can I prevent page from jumping after form submit?

I'm using a Shopify Theme which I've been editing as required. 我使用的是Shopify主题,该主题已根据需要进行编辑。 I have chosen to move the newsletter subscription form from the bottom of the page to the top so I can embed it into the hero image. 我选择将新闻通讯订阅表单从页面底部移至顶部,以便将其嵌入英雄图像中。 When the user submits the form the page jumps to the bottom (where the form used to be). 当用户提交表单时,页面跳至底部(表单以前位于该位置)。 Below is the website and the code for the form. 以下是网站和表单代码。

www.hoo-gah.co.uk www.hoo-gah.co.uk

<div class="signup-form">
           {% if settings.footer_signup_action == empty %}
               {% form 'customer' %}
             {% if form.posted_successfully? %}
               <h4>{{ 'layout.signup.post_success' | t }}</h4>
               <script>window.scroll(0,1000000)</script>
             {% else %}
                   <input type="hidden" id="contact_tags" name="contact[tags]" value="prospect,newsletter"/>
                   <span class="input-block">
                     <input type="email" placeholder="{{ 'layout.signup.email' | t }}" class="required" value="" id="mailinglist_email" name="contact[email]" /><input class="compact" type="submit" value="Subscribe" />
                   </span>
             {% if form.errors %}
                   {% for field in form.errors %}
                   <p class="error">{{ field | capitalize }} - {{ form.errors.messages[field] }}</p>
                   {% endfor %}
             {% endif %}
            {% endif %}
           {% endform %}
           {% else %}
           <form action="{{ settings.footer_signup_action }}" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank">
            <span class="input-block hero-input">
              <input type="email" class="required email" placeholder="{{ 'layout.signup.email' | t }}" value="" id="mce-EMAIL" required name="EMAIL" /><input class="compact" type="submit" name="subscribe" value="Subscribe" />
            </span>
           </form>
          {% endif %}
</div>

I thought that the line window.scroll(0,1000000) would be responsible for this, but even when I delete the line or change the values to (0,0) the screen still jumps upon submit, so I'm wondering if this is being overridden somewhere. 我以为window.scroll(0,1000000)行会对此负责,但是即使删除行或将值更改为(0,0)时,屏幕仍会在提交时跳转,所以我想知道这是否被某个地方覆盖。

If anyone has any ideas on how to make the page scroll to the top instead of to the bottom, it would be much appreciated. 如果有人对如何使页面滚动到顶部而不是底部有任何想法,将不胜感激。

Thanks for your time! 谢谢你的时间!

Remove the #contact_form from action="/contact#contact_form" in your form element, and life will be beautiful again. 从表单元素中的action="/contact#contact_form"中删除#contact_form ,生活将再次美好。

The #contact_form tells the browser to scroll to the form.Removing it will have no other affect. #contact_form告诉浏览器滚动到该表单。删除该表单不会有其他影响。

Just to be clear this is the form to change: 为了清楚起见,这是要更改的形式:

<form method="post" action="/contact#contact_form" id="contact_form" class="contact-form" accept-charset="UTF-8"><input type="hidden" value="customer" name="form_type"><input type="hidden" name="utf8" value="✓">

                       <input type="hidden" id="contact_tags" name="contact[tags]" value="prospect,newsletter">
                       <span class="input-block">
                         <input type="email" placeholder="Enter your email..." class="required" value="" id="mailinglist_email" name="contact[email]"><input class="compact" type="submit" value="Subscribe">
                       </span>


                       <p class="error">Email - can't be blank</p>



                </form>

Should be: 应该:

<form method="post" action="/contact" id="contact_form" class="contact-form" accept-charset="UTF-8"><input type="hidden" value="customer" name="form_type"><input type="hidden" name="utf8" value="✓">

                       <input type="hidden" id="contact_tags" name="contact[tags]" value="prospect,newsletter">
                       <span class="input-block">
                         <input type="email" placeholder="Enter your email..." class="required" value="" id="mailinglist_email" name="contact[email]"><input class="compact" type="submit" value="Subscribe">
                       </span>


                       <p class="error">Email - can't be blank</p>



                </form>

I know this is old but I encountered the same issue today. 我知道这很旧,但是今天我遇到了同样的问题。 I have to answer because I can't just comment yet. 我必须回答,因为我还不能发表评论。 Also, based on O_Z answer : 另外,基于O_Z答案:

You should replace this piece of code : 您应该替换这段代码:

 {% form 'customer' %}

by this one 通过这个

{% form 'customer', action:'/contact' %}

That's the proper way to do it I guess. 我猜那是正确的方法。 It is not well (or at all) documented in Shopify, but you can add elements (action, classes, data-type etc.) directly from your html file, just like the above example. Shopify中没有很好地记录(或根本没有记录),但是您可以直接从html文件中添加元素(动作,类,数据类型等),就像上面的示例一样。 In my case, after the form is submitted, the page stays on top by default and no longer scrolls. 就我而言,提交表单后,默认情况下页面位于顶部,并且不再滚动。

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

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