简体   繁体   English

确认表单并显示消息(如果表单对JQuery有效)

[英]Confirm a form & display message if form is valid with JQuery

I'm developing my Django application and I would like to use JavaScript in order to improve my website. 我正在开发Django应用程序,并且想使用JavaScript来改善我的网站。

I have a form and I would like to display 2 things : 我有一个表格,我想显示两件事:

  • Confirm the form before submit 提交前确认表格
  • If form is well submitted, display a message : 'Form is saved' 如果表单提交正确,则显示一条消息:“表单已保存”

It's the first time I'm using JS and I need help to make this process. 这是我第一次使用JS,我需要帮助来完成此过程。

This is my code : 这是我的代码:

    <form class = "form" method='POST' action=''> {% csrf_token %}
        <br></br>
        {{ form.as_p}}
        <br></br>

        <button type="submit">Valider</button>
    </form>

    <script>
        $('#form').submit(function() {
        var c = confirm("Click OK to continue?");
        return c; //you can just return c because it will be true or false
    });
    </script>

And if my form is valid and saved : 如果我的表格有效并已保存:

<script type="text/javascript" >
                $(document).on('Valider', 'form.form', function(form) {
                var $form = $(form);
                $.ajax({
                    url:"/path_to_my_html_file/BC_form2.html",
                    type: "POST",
                    success: function(form) {
                    alert("Form is saved");
                    }
                });
                });
            </script>

Could you help me ? 你可以帮帮我吗 ?

Thank you 谢谢

You can try to adopt by your purpose this code: 您可以尝试根据自己的目的采用此代码:

 $('#form').submit(function(e) { // Prevents form to be submitted by default post request with page reloading e.preventDefault(); if (confirm("Click OK to continue?")) { // Here you can call your AJAX request callAjax($('input[type=text]').val()) } }); function callAjax(value) { // Making AJAX request to your endpoint // GET ipify just for example $.ajax({ url:"https://api.ipify.org?format=json", type: "GET", success: function(form) { alert("Form is saved"); } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="form"> <input type="hidden" value="MOCK FOR CSRF TOKEN" /> <br></br> <input type="text" required /> <br></br> <button type="submit">Valider</button> </form> 

Use .valid() in submit function 在Submit函数中使用.valid()

$('#form').submit(function() {
  var isValid=$("#form").valid();
           if (isValid) { //If there is no validation error

            var c = confirm("Click OK to continue?");
            if(c){
              $.ajax({
                url:"/path_to_my_html_file/BC_form2.html",
                type: "POST",
                success: function(form) {
                  alert("Form is saved");
                }
              });

            }

          } 
          else {
            alert('form is not valid');                               
          }

        });
</script>

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

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