jQuery: submiting form doesn't work after preverDefault() - javascript

<form class="signup nomargin sky-form boxed" id="signup_form" method="post" action="/accounts/signup/" autocomplete="off">
<input type="hidden" name="csrfmiddlewaretoken" value="H0nDydQL7RNNAStNwIRRILxa4WAVLExq">
<header>
<i class="fa fa-users"></i> 회원가입
</header>
<fieldset class="nomargin ">
<div id="div_id_username" class="form-group">
<div class="controls ">
<input class="textinput textInput form-control" id="id_username" maxlength="30" name="username" placeholder="아이디" type="text">
</div>
</div>
<div id="div_id_email" class="form-group">
<div class="controls ">
<input class="textinput textInput form-control" id="id_email" name="email" placeholder="이메일" type="email">
</div>
</div>
<div id="div_id_password1" class="form-group">
<div class="controls ">
<input class="textinput textInput form-control" id="id_password1" name="password1" placeholder="비밀번호" type="password">
</div>
</div>
<div id="div_id_password2" class="form-group">
<div class="controls ">
<input class="textinput textInput form-control" id="id_password2" name="password2" placeholder="비밀번호 확인" type="password">
</div>
</div>
<div class="note clearfix text-center margin-top-20">
<input type="checkbox" id="usage-agree"> Spacegraphy 이용약관 및 개인정보 취급방침에 동의합니다
</div>
<div class="clearfix note margin-bottom-10 text-center"> <br>
이미 계정이 있으시다구요? <br>
</div>
</fieldset>
<div class=" text-center">
<input type="submit" name="submit" value="가입하기" class="btn btn-primary btn btn-primary noradius" id="signup-btn" style="width:90%; margin:0px 0px 30px 0px;">
</div>
</form>
register.js
$(document).ready(function(){
$("#signup-btn").click(function(event){
event.preventDefault();
if ($("#usage-agree").prop('checked') != true) {
alert("이용약관에 동의해주세요");
} else {
$("#signup_form").submit();
}
});
});
If I would not check the #usage-agree checkbox, it shows alert.
But If I would, it didn't do anything. Nothing happened!
I want to submit the form if checkbox is checked.
I don't know what is wrong with my code.

It is because your have e.preventDefault() on click which prevent form submit.
$(document).ready(function(){
$("#signup-btn").click(function(event){
event.preventDefault(); // this prevent your form submit
if ($("#usage-agree").prop('checked') != true) {
alert("이용약관에 동의해주세요");
} else {
$("#signup_form").submit();
}
});
});
Instead you should do as follows,
$(document).ready(function(){
$("#signup-btn").click(function(event){
if ($("#usage-agree").prop('checked') != true) {
event.preventDefault(); //only if unchecked prevent
alert("이용약관에 동의해주세요");
} else {
$("#signup_form").submit();
}
});
});

Related

Display a part of the page by the hash in the url using jQuery

Well, this project came to my mind cause of this problem:
When I hit the register button, the PHP script is gonna process it and it displays error but the page is gonna be at the login form.
The interface:
The idea now:
If the url has some parameter in it, process it into the jQuery, and display the requested page from the url.
Example: someurl.com/#login - then display the Login Page
someurl.com/#register - then display the Register Page
Tried solution (jQuery):
$(function() {
// get the `hash` from the url
var url = window.location.hash;
// display the page
$(url).show();
}
My codes right now:
The script:
$(function() {
var url = window.location.hash;
$(url).show();
$('#login-form-link').click(function(e) {
$("#login-form").delay(100).fadeIn(100);
$("#register-form").fadeOut(100);
$('#register-form-link').removeClass('active');
$(this).addClass('active');
e.preventDefault();
$(document).prop('title', 'Login | Prospekt');
});
$('#register-form-link').click(function(e) {
$("#register-form").delay(100).fadeIn(100);
$("#login-form").fadeOut(100);
$('#login-form-link').removeClass('active');
$(this).addClass('active');
e.preventDefault();
$(document).prop('title', 'Register | Prospekt');
});
});
The forms:
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-login">
<div class="panel-heading">
<div class="row">
<div class="col-xs-6">
Login
</div>
<div class="col-xs-6">
Register
</div>
</div>
<hr>
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
<form id="login-form" action="http://phpoll.com/login/process" method="post" role="form" style="display: block;">
<div class="form-group">
<input type="text" name="username" id="username" tabindex="1" class="form-control" placeholder="Username" value="">
</div>
<div class="form-group">
<input type="password" name="password" id="password" tabindex="2" class="form-control" placeholder="Password">
</div>
<div class="form-group text-center">
<input type="checkbox" tabindex="3" class="" name="remember" id="remember">
<label for="remember"> Remember Me</label>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit" name="login-submit" id="login-submit" tabindex="4" class="form-control btn btn-login" value="Log In">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-12">
<div class="text-center">
Forgot Password?
</div>
</div>
</div>
</div>
</form>
<form id="register-form" action="http://phpoll.com/register/process" method="post" role="form" style="display: none;">
<div class="form-group">
<input type="text" name="username" id="username" tabindex="1" class="form-control" placeholder="Username" value="">
</div>
<div class="form-group">
<input type="email" name="email" id="email" tabindex="1" class="form-control" placeholder="Email Address" value="">
</div>
<div class="form-group">
<input type="password" name="password" id="password" tabindex="2" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<input type="password" name="confirm-password" id="confirm-password" tabindex="2" class="form-control" placeholder="Confirm Password">
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit" name="register-submit" id="register-submit" tabindex="4" class="form-control btn btn-register" value="Register Now">
</div>
</div>
</div>
</form>
</div>
</div>
<!--
<hr>
<div class="alert alert-warning alert-dismissible">
×
Username is required! <br>
Password is required!
</div>
-->
</div>
</div>
</div>
</div>
</div>
After you get your hash from the url, call the click() event corresponding to that hash after declaring the click handlers.
$(function() {
$('#login-form-link').click(function(e) {
$("#login-form").delay(100).fadeIn(100);
$("#register-form").fadeOut(100);
$('#register-form-link').removeClass('active');
$(this).addClass('active');
e.preventDefault();
$(document).prop('title', 'Login | Prospekt');
});
$('#register-form-link').click(function(e) {
$("#register-form").delay(100).fadeIn(100);
$("#login-form").fadeOut(100);
$('#login-form-link').removeClass('active');
$(this).addClass('active');
e.preventDefault();
$(document).prop('title', 'Register | Prospekt');
});
var url = window.location.hash;
$(url + '-form-link').click();
});

How to refer input id with a div id in javascript

Here is my div image:
I want to refer input id within gallery-1 dive. I tried this script:
$(document).ready(function () {
document.getElementById('#gallery-1','id_gallery_set-1-title').onchange = function() {
var e = document.getElementById('#gallery-1','id_gallery_set-1-slug');
if (!e._changed) { e.value = URLify(document.getElementById('#gallery-1','id_gallery_set-1-title').value, 50); }
}
});
But it did not work. How do I refer input id='id_gallery_set-1-title' within gallery-1 div?
Edit:
This code work for first formset:
$(document).ready(function () {
document.getElementById('id_gallery_set-0-title').onchange = function() {
var e = document.getElementById('id_gallery_set-0-slug');
if (!e._changed) { e.value = URLify(document.getElementById('id_gallery_set-0-title').value, 50); }
}
});
MY form html:
<form action="." method="post" enctype="multipart/form-data">
{{ formset.management_form }}
{% csrf_token %}
<legend>Event</legend>
<div class="event">
{{ form|crispy}}
</div>
<legend>
<div class="pull-right"><i class="icon-plus icon-white"></i> Add Photo</div>
Photo Gallery
</legend>
<div class="gallery form-inline">
{% for form in formset %}
{{ form|crispy }}
{% endfor %}
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
This is my full inspect of form:
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 col-md-8">
<div class="panel panel-default">
<div class="panel-body">
<form action="." method="post" enctype="multipart/form-data">
<input id="id_gallery_set-TOTAL_FORMS" name="gallery_set-TOTAL_FORMS" value="2" type="hidden"><input id="id_gallery_set-INITIAL_FORMS" name="gallery_set-INITIAL_FORMS" value="0" type="hidden"><input id="id_gallery_set-MIN_NUM_FORMS" name="gallery_set-MIN_NUM_FORMS" value="1" type="hidden"><input id="id_gallery_set-MAX_NUM_FORMS" name="gallery_set-MAX_NUM_FORMS" value="1000" type="hidden">
<input name="csrfmiddlewaretoken" value="QrSlXahV8qshQCefhNwcF1wrd4eWfDuX0UTslT3lYsyheDCdJ8GQrcBQ0IPrYqiK" type="hidden">
<legend>Event</legend>
<div class="event">
<div id="div_id_name" class="form-group"> <label for="id_name" class="control-label requiredField">
Name<span class="asteriskField">*</span> </label> <div class="controls "> <input class="textinput textInput form-control" id="id_name" maxlength="31" name="name" required="" type="text"> </div> </div> <div id="div_id_description" class="form-group"> <label for="id_description" class="control-label requiredField">
Description<span class="asteriskField">*</span> </label> <div class="controls "> <textarea class="textarea form-control" cols="40" id="id_description" name="description" rows="10" required=""></textarea> </div> </div> <div id="div_id_tags" class="form-group"> <label for="id_tags" class="control-label ">
Tags
</label> <div class="controls "> <select multiple="multiple" class="selectmultiple form-control" id="id_tags" name="tags">
</select> </div> </div>
</div>
<legend>
<div class="pull-right"><i class="icon-plus icon-white"></i> Add Photo</div>
Photo Gallery
</legend>
<div class="gallery form-inline">
<div id="div_id_gallery_set-0-title" class="form-group"> <label for="id_gallery_set-0-title" class="control-label requiredField">
Title<span class="asteriskField">*</span> </label> <div class="controls "> <input class="textinput textInput form-control" id="id_gallery_set-0-title" maxlength="35" name="gallery_set-0-title" type="text"> </div> </div> <div id="div_id_gallery_set-0-slug" class="form-group"> <label for="id_gallery_set-0-slug" class="control-label requiredField">
Slug<span class="asteriskField">*</span> </label> <div class="controls "> <input class="textinput textInput form-control" id="id_gallery_set-0-slug" maxlength="35" name="gallery_set-0-slug" readonly="readonly" type="text"> </div> </div> <div id="div_id_gallery_set-0-image" class="form-group"> <label for="id_gallery_set-0-image" class="control-label ">
Image
</label> <div class="controls "> <input class="clearablefileinput" id="id_gallery_set-0-image" name="gallery_set-0-image" type="file"> <p id="hint_id_gallery_set-0-image" class="help-block">Event Picture</p> </div> </div> <input id="id_gallery_set-0-id" name="gallery_set-0-id" type="hidden"> <div class="form-group"> <div id="div_id_gallery_set-0-DELETE" class="checkbox"> <label for="id_gallery_set-0-DELETE" class=""> <input class="checkboxinput" id="id_gallery_set-0-DELETE" name="gallery_set-0-DELETE" type="checkbox">
Delete
</label> </div> </div> <input id="id_gallery_set-0-event" name="gallery_set-0-event" type="hidden">
<div id="gallery-1">
<label for="id_gallery_set-1-title">Title:</label><input id="id_gallery_set-1-title" maxlength="35" name="gallery_set-1-title" type="text">
<label for="id_gallery_set-1-slug">Slug:</label><input id="id_gallery_set-1-slug" maxlength="35" name="gallery_set-1-slug" readonly="readonly" type="text">
<label for="id_gallery_set-1-image">Image:</label><input id="id_gallery_set-1-image" name="gallery_set-1-image" type="file"><br><span class="helptext">Event Picture</span>
<label for="id_gallery_set-1-DELETE">Delete:</label><input id="id_gallery_set-1-DELETE" name="gallery_set-1-DELETE" type="checkbox"><input id="id_gallery_set-1-id" name="gallery_set-1-id" type="hidden"><input id="id_gallery_set-1-event" name="gallery_set-1-event" type="hidden">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
Console errors:
Unknown property ‘-moz-outline’. Declaration dropped.create:1:4347
Unknown property ‘-moz-box-shadow’. Declaration dropped.create:1:5300
Expected declaration but found ‘*’. Skipped to next declaration.create:1:6215
Expected ‘none’, URL, or filter function but found ‘alpha(’. Error in parsing value for ‘filter’. Declaration dropped.create:1:9330
<div id="gallery-1">
<tr><th><label for="id_gallery_set-1-title">Title:</label></th><td><input id="id_gallery_set-1-title" maxlength="35" name="gallery_set-1-title" type="text" /></td></tr>
<tr><th><label for="id_gallery_set-1-slug">Slug:</label></th><td><input id="id_gallery_set-1-slug" maxlength="35" name="gallery_set-1-slug" readonly="readonly" type="text" /></td></tr>
<tr><th><label for="id_gallery_set-1-image">Image:</label></th><td><input id="id_gallery_set-1-image" name="gallery_set-1-image" type="file" /><br /><span class="helptext">Event Picture</span></td></tr>
<tr><th><label for="id_gallery_set-1-DELETE">Delete:</label></th><td><input id="id_gallery_set-1-DELETE" name="gallery_set-1-DELETE" type="checkbox" /><input id="id_gallery_set-1-id" name="gallery_set-1-id" type="hidden" /><input id="id_gallery_set-1-event" name="gallery_set-1-event" type="hidden" /></td></tr>
</div>
photo.js:8:13
Use of getPreventDefault() is deprecated. Use defaultPrevented inste
You can try this clean syntax like:
$(document).ready(function () {
$('body').on('input','div[id^="gallery-"] input[id$="title"]',function() {
el = $(this).parent().find('> input[id$="slug"]');
el.val( URLify($(this).val(),50))
});
});
or change the ids to classes
note put this syntax in a script tag at the bottom of the page
$(document).ready(function() {
$('body').on('input','div[id^="gallery-"] input[id$="title"]',function() {
el = $(this).parent().find('> input[id$="slug"]');
el.val($(this).val() + 'xxxxxxxxxxxx');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body>
<div id="gallery-1">
<input type="text" id="id_gallery_set-1-title">
<input type="text" id="id_gallery-1-slug">
<div id="gallery-2">
<input type="text" id="id_gallery_set-2-title">
<input type="text" id="id_gallery-2-slug">
</div>
</div>
<div id="gallery-3">
<input type="text" id="id_gallery_set-3-title">
<input type="text" id="id_gallery-3-slug">
</div>
</body>
</html>
You don't need to use parent selector #gallery-1 because you have id. Id must be unique for all DOM elements.
I don't know what is e._changed and what you want to do with this check, so I'm sure that if(!e._changed) will always returns true.
$(document).ready(function () {
document.querySelector('#id_gallery_set-1-title').addEventListener('change', function() {
var e = document.querySelector('#id_gallery_set-1-slug');
if (!e._changed) {
e.value = URLify(document.querySelector('#id_gallery_set-1-title').value, 50); }
}
});
It should help you. whenever text change in textbox event occure in that text box refere by child of div with id gallery-1
$("#gallery-1 > #id_gallery_set-1-title").on("input",function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body>
<div id="gallery-1">
<input type="text" id="id_gallery_set-1-title">
<input type="button" value="submit">
</div>
</body>
</html>
You could use querySelector() :
document.querySelector('#gallery-1 #id_gallery_set-1-title');
Not sure what you're trying to achieve but since you're using jQuery you could attach event like :
$(document).ready(function () {
$('#gallery-1 #id_gallery_set-1-title').on('input', function(){
//Get click element using '$(this)'
if( CONDITION ){
}
});
});
Hope this helps.

ng-disabled in Angular JS

I have a modal popup with few mandatory fields.
<form name="myReferenceDataForm" role="form" ng-submit="AddNewGroupMembershipReferenceRecord()" style="margin: 10px">
<div class="form-group row">
<div style="padding-bottom:5px; padding-top:5px; ">
<label for="GroupCode" class="col-sm-4 form-control-label">
Group Code <span class="requiredRed">*</span>
</label>
<div class="col-sm-8">
<input ng-model="referenceAddRecord.groupCode" class="form-control" id="GroupCode" type="text">
</div>
</div>
</div>
<div class="form-group row">
<div style="padding-bottom:5px; padding-top:5px; ">
<label for="GroupName" class="col-sm-4 form-control-label">
Group Name <span class="requiredRed">*</span>
</label>
<div class="col-sm-8">
<input ng-model="referenceAddRecord.groupName" id="GroupName" class="form-control" type="text">
</div>
</div>
</div>
<div class="form-group row">
<label for="GroupType" class="col-sm-4 form-control-label">Group Type </label>
<div class="col-sm-8">
<select name="selGroupType" id="selGroupType" class="form-control" ng-change="referenceAddRecord.populateGroupTypeDetails(selGroupType)" ng-options="groupType.value for groupType in referenceAddRecord.groupTypes track by groupType.id" ng-model="referenceAddRecord.groupType"></select>
</div>
</div>
Only above 3 fields groupName , groupCode are mandatory.
If they exist or not filled I want to have the submit button in the page disabled.
So , I did ...
<input type="submit" class="btn btn-default pull-right" style="width:100px;" value="Submit" ng-disabled="!(referenceAddRecord.groupCode || referenceAddRecord.groupName)|| addReferenceDataGroupNameExists() || addReferenceDataGroupCodeExists()" />
But it is not working . Even Only the whether the fields are filled that check is failing. Whenever I enter only one field it is getting enabled.
<input type="submit" class="btn btn-default pull-right" style="width:100px;" value="Submit" ng-disabled="!(referenceAddRecord.groupCode || referenceAddRecord.groupName)" />
What am I doing wrong here ?
Check out this plunker
<input type="submit" id="submit" ng-disabled="!(referenceAddRecord.groupName && referenceAddRecord.groupCode)" value="Submit" />

stop refresh when form is submitted if any error and refresh if no error

I have 2 forms log in and sign up written on same index.php and they toggle according to the users need.The log in form appears when you open index.php while the sign up form is hidden and appears only when you click on signup here link.
Now my issue is that while signing up if there is any error(validation,database error) then the page refreshes when submit button is clicked obviously and returns to my login form which appears when you open/refresh index.php.Again I have to click on signup here link to solve the errors.
I just want to stay on signup form when errors appear that means I dont want the page to be refreshed when clicked on submit but want the errors to appear and solve them then and there.
$.ajax({
type: "POST",
url: "phpindex.php",
data: $("#signUpForm").serialize(),
success:function(data)
{
if ("#error".val(data))
{
e.preventDefault();
}
}
});
});
My validation and database code is written in phpindex.php
***************signup form************************
<form method="post" id="signUpForm">
<div class="container" id="signupContainer">
<h1>Sign Up</h1>
<div id="error"><?php if ($error!="") {
echo '<div class="alert alert-danger" role="alert">'.$error.'</div>';
} ?></div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" placeholder="Your name">
<span class="fa fa-user"></span>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">Address1</label>
<div class="col-sm-10">
<input type="text" id="address1" class="form-control" name="address1" placeholder="Home address">
<span class="fa fa-map-marker"></span>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">Address2</label>
<div class="col-sm-10">
<input type="text" id="address2" class="form-control" name="address2" placeholder="City,Pincode....">
<span class="fa fa-map-marker"></span>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">Email</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="email" placeholder="Email Address">
<span class="fa fa-envelope"></span>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="password" placeholder="Password">
<span class="fa fa-key"></span>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-5 col-sm-10">
<input type="submit" id = "submit1"class="btn btn-success btn-lg" value="Sign In" name="submit">
</div>
</div>
<p><a class="toggleForms">Back to Log in</a></p>
</div>
</form>
***************login form*****************
<form method="post" id="logInForm">
<div class="container" id="logInContainer">
<h1>Log In</h1>
<div id="success"><?php if ($success!="") {
echo '<div class="alert alert-success" role="alert">'.$success.'</div>';
} ?></div>
<div class="form-group row">
<label class="col-sm-3 form-control-label">Emai</label>
<div class="col-sm-8">
<input type="email" class="form-control" placeholder="Email" name="email">
<span class="fa fa-envelope"></span>
</div>
</div>
<div class="form-group row">
<label class="col-sm-3 form-control-label">Password</label>
<div class="col-sm-8">
<input type="password" class="form-control" placeholder="Password" name="password">
<span class="fa fa-key"></span>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-4 col-sm-10">
<input type="hidden" name="signUp" value="0">
<input type="submit" id = "submit2" class="btn btn-success btn-lg" value="Log In" name="submit">
</div>
</div>
<p><a class="toggleForms">Sign Up Here</a></p>
</div>
</form>
Firstly, there are numerous errors in the code you have submitted. I believe they are mainly due to clumsy copy and pasting, but there is one that I think you just have in your code, and it is the missing jQuery reference for "#error".
"#error".val(data)
I think you wanted to write something like this
$("#error").val(data)
This error would stop "preventDefault" from happening.
Secondly, I wonder where is your submit handler for those forms and how it looks like. If you have none, then there is you answer. You just do not catch the submit event.
Lastly, I would consider calling, instead of preventDefault:
return false
Please see this thread as for the reasons why
event.preventDefault() vs. return false

Form submits even when i use prevent default on submit button click

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:-
<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:-
<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>
You need to put your code in a document.ready() function, so:
$(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"...
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.

Categories