使用jquery关闭模式弹出窗口时刷新页面

时间:2016-11-18 06:17:12

标签: jquery twitter-bootstrap

我的问题:

我有一个模态表单,当我点击提交按钮时弹出。模态表单允许用户填写一组字段并请求组织演示。然后使用codeigniter将表单数据插入到mysql数据库中。当用户提交经过验证的表单时,模态弹出窗口只显示成功消息和动态生成的按钮(替换上一个表单),允许用户关闭模态表单。

当我点击动态生成的关闭按钮时,模式窗体会关闭并刷新页面。

但是,当我点击模态表单右上角的关闭图标时,我无法做同样的事情(刷新页面)。

我尝试使用以下代码,但我无法在模式弹出窗口的close事件上刷新页面。

$('#mymodal').on("hidden", function() {
          location.reload();;
         });

我也尝试使用以下代码:

$('#mymodal').on("hidden.bs.modal", function() {
              location.reload();;
             });

我已使用 jquery.mask.js jquery.validate.min.js 进行模式表单的表单验证。

我的研究:

我在Stack Overflow上经历了以下问题:

我的模态形式:

<div class="container">
<!-- <form name="" id="" action="" method="get"> -->
<!-- Trigger the modal with a button -->
  <div class="col-md-2 col-md-offset-5">
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" style="margin:0 auto;">Request a Demo</button>
  </div>
  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">  
    <div class="modal-dialog" id="idmodaldialog">    
      <!-- Modal content-->
      <form name="frmdemo" id="idfrmdemo" method="post">
      <div class="modal-content">        
         <div class="modal-header" align="center">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title"><strong>Request a Demo</strong></h4>
        </div>
        <div class="modal-body" id="idmodalcontent">                         
             <div class="row">
             <div class="form-group col-md-6">
             <label for="txtfirstname">FIRST NAME</label>
             <input name="txtfirstname" type="text" class="form-control required" placeholder="Please enter your first name" id="idfirstname" >
             </div>
             <div class="form-group col-md-6">
             <label for="txtlastname">LAST NAME</label>
             <input name="txtlastname" type="text" class="form-control required" placeholder="Please enter your last name" id="idlastname">            
             </div>           
             </div>             
             <div class="form-group">
             <label for="Email">EMAIL</label>
             <input type="email" name="txtemail" class="form-control" placeholder="Please enter your email id" id="idemail">
             <div class="divstyle">(Please enter your email id in block letters)</div>
             </div>             
             <div class="form-group">
             <label for="idcompany">COMPANY</label>
             <input type="text" name="txtcompany" placeholder="Please enter the name of your company" class="form-control" id="idcompany" >
             </div>             
             <div class="form-group">
             <label for="Phone">PHONE</label>
             <input type="tel" name="txtphone" placeholder="Please enter your phone number" class="form-control" id="idphone">             
             </div>
            <div class="form-group">
             <label for="">MODE OF CONTACT</label>
             <select name="selmode" id="idmode" class="form-control">
              <option value="">Select</option>
              <option value="email">Email</option>
            <option value="phone">Phone</option>  
            </select>
             </div>         
         </div>
        <div class="modal-footer">
          <input type="submit" id="idsubmit" name="submit" value="Submit" class="btn btn-warning" />
        </div>
      </div>
      </form> 
    </div>
  </div> 
</div>

我的Jquery表格验证码:

// Wait for the DOM to be ready
$(function() {  

    $('#mymodal').on("hidden", function() {
          location.reload();;
         });    

    $('#idphone').mask('(000) 000-0000');

    // Initialize form validation on the registration form.
  // It has the name attribute "frmdemo"
  $("form[name='frmdemo']").validate({
    // Specify validation rules 
      rules: {
      // The key name on the left side is the name attribute
      // of an input field. Validation rules are defined
      // on the right side
      "txtfirstname": {required:true,firstnameval:true},
      "txtlastname": {required:true,lastnameval:true},
      "txtemail": {
        required: true,        
        // Specify that email should be validated
        // by the built-in "email" rule
        email: true,
        emailval:true 
      },
      "txtcompany": {
          required: true,
          companyval:false    
      },

      "txtphone" : {
         required : true,
         usphoneval:true 
      },   

      "selmode" :{
         required : true 
      } 
    },
    // Specify validation error messages
    messages: {
      txtfirstname: {
          required:"Please enter your first name" ,
          firstnameval:"Enter proper first name"
          //firstnameval:"Enter only alphabets ",         
            },
     txtlastname: {
                  required:"Please enter your last name " ,
                  lastnameval:"Enter proper last name"
                  //firstnameval:"Enter only alphabets ",         
                    },    
      txtemail: {
                  required : "Please enter a valid email address",     
                  emailval : "Please enter your corporate email id"       
      },     
      txtcompany: {
          required :"Please enter the name of your company",
          companyval:"Enter a proper company name"
      },         
      txtphone: {
          required :"Please enter your phone number",
          usphoneval :"Please enter a valid phone number"  
      },
         selmode:"Please select the mode in which we should contact you"
    },

    // Make sure the form is submitted to the destination defined
    // in the "action" attribute of the form when valid  

    submitHandler: function(form) {
       // form.submit();
      $('#idmodalcontent').html('Form submitted successfully');

      var input = $('<input/>').attr({ type: 'button', name:'btnsubmit', value:'Close', id:'idclose',"class":"btn btn-warning"});    

      //"data-dismiss":'modal',

      $(".modal-footer").html(input);

      $("#idclose").on("click",function(){
         location.reload();
        });              

      /*$('#idmodalcontent').modal({
              onClose: function(dialog){
                location.reload(true);
            }
        }); 
      */
      return false;        
    }        
  });


//add your own custom validation rule

  //US phone format
    $.validator.addMethod("usphoneval", function(value,element)
    {       
        //^\(?([d]{3})\)?[. ]?([d]{3})[. ]?([d]{4})$

        if (/^\(?([0-9]{3})\)?[ ]?([0-9]{3})[-]?([0-9]{4})$/.test(value)) 
        {
            return true;

        } else {

            return false;           
        }       

    },function() 
    { 
    });


    //first name : only alphabets
    $.validator.addMethod("firstnameval", function(value,element)
            {       
                //^\(?([d]{3})\)?[. ]?([d]{3})[. ]?([d]{4})$

                if (/^[a-zA-Z]{1,256}$/.test(value)) 
                {
                    return true;

                } else {

                    return false;           
                }       

            },function() 
            { 
            });

    $.validator.addMethod("lastnameval", function(value,element)
            {       

                //first character alphabet - rest alphanumeric
                if (/^[a-zA-Z][a-zA-Z0-9]{0,256}$/.test(value)) 
                {

                    return true;


                } else {

                    return false;
                }       

            },function() 
            {           
            });

    //validate company - first character alphabet,rest only alphabet,dot,spaces
    $.validator.addMethod("companyval", function(value,element)
            {       
                if (/^[a-zA-Z][a-zA-Z. ]{1,256}$/.test(value)) 
                {
                    return true;                    

                } else {

                    return false;
                }       

            },function() 
            { 
            });     

    $.validator.addMethod("emailval", function(value,element)
            {       

            var n = value.lastIndexOf('@');
            var extension = value.substring(n+1).toLowerCase();
            if (extension == "gmail.com" || extension=="aol.com"|| extension=="facebook.com"|| extension=="googlemail.com"                  
            || extension=="hotmail.com" || extension=="hotmail.co.uk" || extension=="yahoo.com" || extension=="yahoo.co.uk"
            || extension=="live.com"    || extension=="att.net" || extension=="comcast.net" || extension=="gmx.com"     
            || extension=="mac.com" || extension=="me.com" || extension=="sbcglobal.net" || extension=="verizon.net"                
            || extension=="msn.com" || extension=="mail.com"|| extension=="email.com" || extension=="games.com"
            || extension=="gmx.net" || extension=="hush.com" ) {

                return false ;

            } else {

                return true;

            }

            },function() 
            { 
            }); 
});

2 个答案:

答案 0 :(得分:0)

Plz试试这段代码: 你的代码中有错误的#mymodel id name。

$('#myModal').on('hidden.bs.modal', function () {
 location.reload();
})

答案 1 :(得分:0)

试试这个,希望它有所帮助!

$(".close").click(function(){
    bootbox.hideAll();
    location.reload();
});