如何在不重新加载页面的情况下显示模态

时间:2016-10-05 05:10:11

标签: javascript php jquery twitter-bootstrap

在这里我做了表单验证,在表单验证后填写所有字段并单击邀请引号按钮意味着我想要show modal,并且我想在填充所有字段后填写模态表单意味着我想要关闭那个模态,但不想刷新页面,在这里我点击邀请报价按钮意味着页面正在刷新,莫代尔没有显示如何做到这一点?



<script>

/*$('#businessForm').submit(function (e) {
  e.preventDefault();
  validateForm();
});*/
function validateForm() {
  var ratesfor = document.forms["myForm"]["ratesfor"].value;
  if (ratesfor == null || ratesfor == "") {
    document.getElementById("rate_err").innerHTML = "Field is empty";
  }
  var no = document.forms["myForm"]["no"].value;
  if (no == null || no == "") {
    document.getElementById("no_err").innerHTML = "Field is empty";
  }

  var breath = document.forms["myForm"]["breath"].value;
  if (breath == null || breath == "") {
    document.getElementById("b_t_err").innerHTML = "Field is empty";
  }

  var height = document.forms["myForm"]["height"].value;
  if (height == null || height == "") {
    document.getElementById("height_err").innerHTML = "Field is empty";
  }

  var truck_type = document.forms["myForm"]["truck_type"].value;
  if (truck_type == null || truck_type == "") {
    document.getElementById("truck_type_err").innerHTML = "Field is empty";
  }

  var datepicker = document.forms["myForm"]["datepicker-13"].value;
  if (datepicker == null || datepicker == "") {
    document.getElementById("pickup_err").innerHTML = "Field is empty";
  }

  var myTime = document.forms["myForm"]["myTime"].value;
  if (myTime == null || myTime == "") {
    document.getElementById("time_err").innerHTML = "Field is empty";
  }

  var from = document.forms["myForm"]["from"].value;
  if (from == null || from == "") {
    document.getElementById("from_err").innerHTML = "Field is empty";

  }

  var to = document.forms["myForm"]["to"].value;
  if (to == null || to == "") {
    document.getElementById("to_err").innerHTML = "Field is empty";
    return false;
  }

  else{

    $('#businessForm').submit(function (e) {
      e.preventDefault();
    })
    $.ajax({
      url:'search_truck.php',
      type:'POST',
      data : { 'state_id' : city},
      success:function(data){
        //var res=jQuery.parseJSON(data);// convert the json
        console.log(data);
      },
    });

  }
}

</script>
<script>
$(document).ready(function(){
    $("#myBtn").click(function(){
        $("#myModal").modal();
    });
});


$(document).ready(function(){
    $("#Register").click(function(){
	 	$("#myModal").hide();
        $("#myModal_Register").modal();
     });
});
</script>
&#13;
<form id="businessForm" method="POST" onsubmit="return validateForm()" name="myForm" enctype="multipart/form-data">

    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <label for="experience">Rates For</label>
                <input type="ratesfor" class="form-control" id="ratesfor" name="ratesfor" placeholder="weight(kg)">
                <span id="rate_err"></span>
            </div>
        </div>

    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="length">Length</label>
                <input type="text" class="form-control" id="no" name="no" placeholder="Length"><span id="no_err"></span>
            </div>
        </div>

        <div class="col-md-6">
            <div class="form-group">
                <label for="length"> </label>
                <select class="form-control form-design col-lg-6 col-md-6 col-sm-12 col-xs-12" autocomplete="off" name="length" id="length" style="margin-top:25px;">
                    <option value="">feet</option>
                    <option value="">mm</option>
                    <option value="">mtr</option>
                </select>

            </div>
        </div>
    </div>

    <div class="row">

        <div class="col-md-6">
            <div class="form-group">
                <label for="breadth">Breadth</label>
                <input type="text" class="form-control" id="breath" name="breath" placeholder="Breadth"><span id="b_t_err"></span>
            </div>
        </div>

        <div class="col-md-6">
            <div class="form-group">
                <label for="breadth"> </label>
                <select class="form-control form-design col-lg-6 col-md-6 col-sm-12 col-xs-12" autocomplete="off" name="b_type" id="b_type" style="margin-top:25px;">
                    <option value="">feet</option>
                    <option value="">mm</option>
                    <option value="">mtr</option>
                </select>
            </div>
        </div>

    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="height">Height</label>
                <input type="text" class="form-control" id="height" name="height" placeholder="Height"><span id="height_err"></span>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="height"> </label>
                <select class="form-control form-design col-lg-6 col-md-6 col-sm-12 col-xs-12" autocomplete="off" name="type" id="h_t" name="h_t" style="margin-top:25px;">
                    <option value="">feet</option>
                    <option value="">mm</option>
                    <option value="">mtr</option>
                </select>

            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <label for="experience">Type Of Truck</label>
                <select id="truck_type" name="truck_type" autocomplete="off" class="form-control form-design col-lg-6 col-md-6 col-sm-12 col-xs-12">
                    <option value="feet">feet</option>
                    <option value="mm">mm</option>
                    <option value="mtr">mtr</option>
                </select><span id="truck_type_err"></span>
            </div>

        </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <label for="date">Date of PickUp</label>
                <br>
                <input type="text" placeholder="DD-MM-YY" id="datepicker-13" name="datepicker-13" style="width:100%;height:35px;">
                <span id="pickup_err"></span>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <label for="time">Time of PickUp</label>
                <br>
                <input type="time" class="form-control" id="myTime" name="myTime" placeholder="Time">
                <span id="time_err"></span>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="experience">Destination</label>
                <select class="form-control form-design col-lg-6 col-md-6 col-sm-12 col-xs-12" autocomplete="off" id="from" name="from">
                    <option value="">From</option>
                    <option value="1">Ahmedabad</option>
                    <option value="2">Bangalore</option>

                </select>
            </div>
            <span id="from_err"></span>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="experience"> </label>
                <select class="form-control form-design col-lg-6 col-md-6 col-sm-12 col-xs-12" autocomplete="off" name="to" id="to" style="margin-top:25px;">
                    <option value="">To</option>
                    <option value="1">Guragon</option>
                    <option value="2">Hyderabad</option>
                </select>
            </div>
            <span id="to_err"></span>
        </div>
    </div>
    <br>


    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <button type="submit" class="btn btn-primary btn-lg btn-block">INVITE QUOTES</button>
            </div>
        </div>
    </div>

</form>


<div class="container">
    <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog" style="z-index: 9999;">

            <!-- Modal content-->
            <div class="modal-content">


                <div class="modal-header" style="padding:5px 50px;">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4>Login</h4>
                </div>
                <div class="modal-body" style="padding:40px 50px;">
                    <form role="form">
                        <div class="form-group">
                            <label for="usrname">Username</label>
                            <input type="text" class="form-control" id="usrname" placeholder="Enter Name">
                        </div>
                        <div class="form-group">
                            <label for="psw"> Password</label>
                            <input type="text" class="form-control" id="psw" placeholder="Enter password">
                        </div>
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" value="" checked>Remember me</label>
                        </div>
                        <button type="submit" class="btn btn-info btn-block">Login </button>
                    </form>
                </div>
                <div class="modal-footer">
                    <p>Not a member? <a href="#" id="Register"><b>Sign Up</b></a>
                    </p>
                    <!--<p>Forgot <a href="#">Password?</a></p>-->
                </div>
            </div>

        </div>
    </div>
    <!--######
                        Login From End
                                 #######--->


    <!--#####
                        Register From Start
                                      #####--->
    <div class="modal fade" id="myModal_Register" role="dialog">
        <div class="modal-dialog" style="z-index: 9999;">

            <!-- Modal content-->
            <div class="modal-content">



                <div class="modal-header" style="padding:5px 50px;">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4>Registration</h4>
                </div>
                <div class="modal-body" style="padding:40px 50px;">
                    <form role="form">
                        <div class="form-group">
                            <label for="usrname"> Username</label>
                            <input type="text" class="form-control" id="usrname" placeholder="Enter name">
                        </div>
                        <div class="form-group">
                            <label for="email">Email</label>
                            <input type="text" class="form-control" id="email" placeholder="Enter email">
                        </div>
                        <div class="form-group">
                            <label for="psw">Password</label>
                            <input type="text" class="form-control" id="psw" placeholder="Enter password">
                        </div>
                        <div class="form-group">
                            <label for="psw"> Confirm Password</label>
                            <input type="text" class="form-control" id="psw" placeholder="Confirm password">
                        </div>

                        <button type="submit" class="btn btn-info btn-block">Submit</button>
                    </form>
                </div>
                <!-- <div class="modal-footer">
              <p>Not a member? <a href="#" id="Register">Sign Up</a></p>
              <p>Forgot <a href="#">Password?</a></p>
            </div>-->
            </div>

        </div>
    </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

兄弟检查这一行:

<button type="submit" class="btn btn-primary btn-lg btn-block">INVITE QUOTES</button>

这里您使用按钮类型提交,提交按钮将提交表单并重新加载页面。而是使用:

<button type="button"  class="btn btn-primary btn-lg btn-block">INVITE QUOTES</button>