即使我在提交按钮单击时使用防止默认,表单也会提交

时间:2022-11-24 09:17:42

what i want is that when i click on button "find reservation", the form submit should not refresh the page. Instead it should enable some fields in find reservation form underneath. But I am not able to achieve that. I am using bootstrap. Here is my html part:-

我想要的是,当我点击“查找预订”按钮时,表单提交不应该刷新页面。相反,它应该在下面的查找预订表单中启用一些字段。但我无法做到这一点。我正在使用bootstrap。这是我的HTML部分: -

<div class="container">
    <div class="jumbotron checkInGuest">
    <h3 class="h3Heading">Request for following information from guest</h3>
    <form class="form-horizontal" id="checkInForm">
        <div class="form-group">
            <label for="reservationId" class="col-md-4">Reservation Id</label>
            <div class="col-md-8">
            <input type="text" class="form-control" id="reservationId" name="reservationId" required>
            </div>
        </div>
        <div class="form-group">
            <label for="dlNum" class="col-md-4">Driver License #</label>
            <div class="col-md-8">
                <input type="number" class="form-control" id="dlNum" min="0" name="dlNum" required>
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-2">
            <button type="submit" class="btn btn-success" id="findResButton">Find Reservation</button>
            </div>
        </div>
    </form>

        <!-- Form when info is found. Initially all fields are disabled-->
    <div class="clear clearWithBorder"></div>
    <h3 class="h3Heading">Information Found</h3>
    <form class="form-horizontal">

        <div class="form-group">
            <label for="resId" class="col-md-3">Reservation Id</label>
            <div class="col-md-3">
                <input type="text" class="form-control" id="resId" name="resId" disabled>
            </div>
            <label for="dlNumReadOnly" class="col-md-3">Driver License #</label>
            <div class="col-md-3">
                <input type="number" class="form-control" id="dlNumReadOnly" min="0" name="dlNumReadOnly" disabled>
            </div>
        </div>

        <div class="form-group">
            <label for="guestFullName" class="col-md-3">Guest Full Name</label>
            <div class="col-md-3">
                <input type="text" class="form-control" id="guestFullName"  name="guestFullName" disabled>
            </div>
            <label for="email" class="col-md-3">Email</label>
            <div class="col-md-3">
                <input type="email" class="form-control" id="email"  name="email" disabled>
            </div>
        </div>

        <div class="form-group">
            <label for="numRooms" class="col-md-3">Rooms Booked</label>
            <div class="col-md-3">
                <input type="number" class="form-control readable" id="numRooms"  name="numRooms" disabled>
            </div>
                <label for="roomType" class="col-md-1">Room Type</label>

                <div class=" col-md-2">
                    <label for="smoking">
                        <input type="radio" name="roomType" id="smoking" class="roomType readable" disabled> Smoking
                    </label>
                </div>
                <div class=" col-md-3">
                    <label for="nonSmoking">
                        <input type="radio" name="roomType" id="nonSmoking" class="roomType readable" disabled>Non-Smoking
                    </label>
                </div>
        </div>
        <div class="form-group">
            <label for="discount" class="col-md-3">Discount</label>
            <div class="col-md-3">
            <select class="form-control readable" id="discount" disabled>
                <option selected>0%</option>
                <option>10%</option>
                <option>20%</option>
                <option>30%</option>
            </select>
                </div>
            <label for="checkInDate" class="col-md-3">CheckIn Date</label>
            <div class="col-md-3">
                <input type="date" class="form-control readable" id="checkInDate"  name="checkInDate" disabled>
            </div>
        </div>

        <div class="form-group">
            <label for="checkOutDate" class="col-md-3">CheckOut Date</label>
            <div class="col-md-9">
                <input type="date" class="form-control readable" id="checkOutDate"  name="checkInDate" disabled>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-2">
                <button type="button" class="btn btn-success" id="roomOrdButton">Confirm Room Order</button>
            </div>
        </div>
    </form>

        <div class="clear clearWithBorder"></div>
        <h3 class="h3Heading">Final Room Order</h3>
        <form class="form-horizontal">
            <div class="form-group">
                <label for="perInEachRoom" class="col-md-12">Number of people in each room</label>
                <div class="col-md-8 " id="perInEachRoom">

                </div>
                <div class="form-group">
                    <div class="col-md-2">
                        <button type="button" class="btn btn-success" id="checkInButton">Check In</button>
                    </div>
                </div>

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

And this is jquery part:-

这是jquery部分: -

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>

    $("#checkInForm").validator();

    $("#findResButton").click(function(e){
        e.preventDefault();
        $(".readable").prop("disabled",false);

    });

    $("#roomOrdButton").click(function(){
        var numberOfRooms=$("#numRooms").val();
        var counter=0;
        var container=$('<div/>');
        $(".readable").prop("disabled",true);
        for(counter=1;counter<=numberOfRooms;counter++){
           container.append('<label for="room">Room '+counter+'</label><input type="number" class="form-control readable"  name="checkInDate" />');
        }
        $("#perInEachRoom").html(container);
    });

    $("#checkInButton").click(function(){
        $("#perInEachRoom").html("");
    });



</script>

2 个解决方案

#1


1  

You need to put your code in a document.ready() function, so:

您需要将代码放在document.ready()函数中,因此:

$(document).ready(function() {
    $("#checkInForm").validator();

    $("#findResButton").click(function(e){
        e.preventDefault();
        $(".readable").prop("disabled",false);

    });

    $("#roomOrdButton").click(function(){
        var numberOfRooms=$("#numRooms").val();
        var counter=0;
        var container=$('<div/>');
        $(".readable").prop("disabled",true);
        for(counter=1;counter<=numberOfRooms;counter++){
           container.append('<label for="room">Room '+counter+'</label><input type="number" class="form-control readable"  name="checkInDate" />');
        }
        $("#perInEachRoom").html(container);
    });

    $("#checkInButton").click(function(){
        $("#perInEachRoom").html("");
    });
});

You should also have a look at this question: Form is submitted when I click on the button in form. How to avoid this?

您还应该看看这个问题:单击表单中的按钮时提交表单。怎么避免这个?

Long story short, you should use

长话短说,你应该用

<button type="button"...

not

<button type="submit"...

#2


0  

Actually, all I did is:-

实际上,我所做的只是: -

$("#checkInForm").submit(function(e)
{
   e.preventDefault();
});

And that solved it. I didnt have to change button type or anything. Basically, in future when I will make AJAX calls I will have the code underneath preventDefault statement.

这解决了它。我没有必要更改按钮类型或任何东西。基本上,将来当我将进行AJAX调用时,我将在preventDefault语句下面有代码。

#1


1  

You need to put your code in a document.ready() function, so:

您需要将代码放在document.ready()函数中,因此:

$(document).ready(function() {
    $("#checkInForm").validator();

    $("#findResButton").click(function(e){
        e.preventDefault();
        $(".readable").prop("disabled",false);

    });

    $("#roomOrdButton").click(function(){
        var numberOfRooms=$("#numRooms").val();
        var counter=0;
        var container=$('<div/>');
        $(".readable").prop("disabled",true);
        for(counter=1;counter<=numberOfRooms;counter++){
           container.append('<label for="room">Room '+counter+'</label><input type="number" class="form-control readable"  name="checkInDate" />');
        }
        $("#perInEachRoom").html(container);
    });

    $("#checkInButton").click(function(){
        $("#perInEachRoom").html("");
    });
});

You should also have a look at this question: Form is submitted when I click on the button in form. How to avoid this?

您还应该看看这个问题:单击表单中的按钮时提交表单。怎么避免这个?

Long story short, you should use

长话短说,你应该用

<button type="button"...

not

<button type="submit"...

#2


0  

Actually, all I did is:-

实际上,我所做的只是: -

$("#checkInForm").submit(function(e)
{
   e.preventDefault();
});

And that solved it. I didnt have to change button type or anything. Basically, in future when I will make AJAX calls I will have the code underneath preventDefault statement.

这解决了它。我没有必要更改按钮类型或任何东西。基本上,将来当我将进行AJAX调用时,我将在preventDefault语句下面有代码。