简体   繁体   English

如何防止我的登录模式在提交时刷新我的网站?

[英]How can I prevent my login modal from refreshing my site on submit?

I'm pretty new to modals (and honestly, webdev in general). 我对模式非常陌生(老实说,一般来说是webdev)。 I suspect this has something to do with my $('#login-modal').submit(); 我怀疑这与我的$('#login-modal').submit();

I have a login modal: 我有一个登录模式:

    <div class="modal fade" id="login-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
        <div class="modal-dialog">
            <div class="loginmodal-container">
                <img src="awslogo.png" alt="" width="80%">
                <form>
                    <input id="awsu" type="text" name="user" placeholder="Username">
                    <input id="awsp" type="password" name="pass" placeholder="Password">
                    <input id="aws-submit" type="submit" name="login" class="form-control login loginmodal-submit" value="Login">
                </form>
            </div>
        </div>
    </div>

And I display it in my js here: 我在这里在我的js中显示它:

this.ctrlNext.addEventListener( 'click', function( ev ) { 
            ev.preventDefault();
            console.log(self.current);
            // display authentication
            if (self.current === 0){
                $('#login-modal').modal('show');
                var awsu = document.getElementById("awsu").value;
                var awsp = document.getElementById("awsp").value;
                console.log(awsu);
                console.log(awsp);
            }

This pops up a nice little login modal: 这会弹出一个漂亮的小登录模式:

模态

The issue I'm running into I suspect revolves around what happens when I click Login: 我怀疑我遇到的问题与单击“登录”时发生的事情有关:

$('#aws-submit').click(function( ev ) {
            //ev.preventDefault();
            console.log("clicked a");
            $('#login-modal').submit();
        });

I tried ev.preventDefault(); 我尝试了ev.preventDefault(); but this just kept the modal up the whole time. 但这只是在整个过程中保持模态不变。 Maybe I need to $('#login-modal').modal('hide'); 也许我需要$('#login-modal').modal('hide'); in here, and somehow grab the data from the form? 在这里,以某种方式从表单中获取数据?

ninja edit -- tried $('#login-modal').modal('hide'); 忍者编辑 -尝试$('#login-modal').modal('hide'); and it seems to work, but I have no earthly idea if I'm doing this the "right" way, and how to get the values properly. 似乎可行,但我不了解是否以“正确”的方式执行此操作以及如何正确获取值。

Try ev.stopPropagation(); 尝试ev.stopPropagation(); .

You can also return false; 您还可以return false; from your handler to stop the event propagation. 从您的处理程序停止事件传播。

When you submit the form, it submits its contents to the action attribute. 提交表单时,它会将其内容提交给action属性。 If no action attribute is set, it submits to itself. 如果未设置任何action属性,它将提交给自己。

ev.preventDefault(); prevents the form from submitting the normal way, and is what you need in this case. 阻止表单以正常方式提交,这是您在这种情况下需要的。 As you noted, it left the modal on the screen, that is because it stopped the page refreshing. 如您所述,它在屏幕上留下了模态,这是因为它停止了页面刷新。 And to get rid of the modal you use $('#login-modal').modal('hide'); 要摆脱模态,可以使用$('#login-modal').modal('hide'); as you thought. 如您所想。

To grab the data you can serialize the form contents. 要获取数据,您可以序列化表单内容。

Give your form an id <form id="login_form"> 给您的表单一个ID <form id="login_form">

Use $('#login_form').serialize(); 使用$('#login_form').serialize(); to serialize the data and then you can use ajax to post the data to the server. 序列化数据,然后可以使用ajax将数据发布到服务器。

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

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