简体   繁体   中英

Javascript to reurn boolean value

I need to create a web page as shown below

在此处输入图片说明

在此处输入图片说明

Below is my current code

 function validate() { var form = document.getElementsByTagName('form')[0]; if (form.tickets.value <= form.childrens.value) { alert("No of tickets should be greater than the no of children"); return false; } else if (new Date(form.showdate.value) < new Date()) { alert("Show date and time should be either current date or future date"); return false; } var ticketFare = (form.tickets.value - form.childrens.value) * 200 + form.childrens.value * 100; alert("Your approximate ticket amount is " + ticketFare + "INR"); return true; } 
 .item img { height: 200px; width: 200px; -webkit-transition: all 2s ease; -moz-transition: all 2s ease; -ms-transition: all 2s ease; transition: all 2s ease; } .item img:hover { width: 300px; height: 300px; } 
 <html> <body> <h1><b>Movie Ticket Booking</b></h1> <form action="thankyou.html" onsubmit="alert(validate());"> <table> <tr> <td>Name</td> <td><input type="text" name="name" placeholder="Enter the name" pattern="[A-za-z\\s]+" required> </td> </tr> <tr> <td>Movie Name</td> <td><input type="text" name="moviename" autocomplete="on" list="movies" required> <datalist id="movies"> <option value="Irada"></option> <option value="Rangoon"></option> <option value="Logan"></option> <option value="Fist Fight"></option> </datalist> </td> </tr> <tr> <td>Circle</td> <td><input type="text" name="circle" autocomplete="on" list="circles" required> <datalist id="circles"> <option value="Silver"></option> <option value="Gold"></option> <option value="Platinum"></option> </datalist></td> </tr> <tr> <td>Phone no</td> <td><input type="text" name="phone" placeholder="Enter mobile # here" pattern="[\\d]{10}" required> </td> </tr> <tr> <td>Show date and time</td> <td><input type="datetime-local" name="showdate" required></td> </tr> <tr> <td>No of tickets</td> <td><input type="number" name="tickets" min="1" max="10" required></td> </tr> <tr> <td>No of children's</td> <td><input type="number" name="childrens" min="1" max="5" required></td> </tr> <tr> <td><input type="submit" value="Book My Show"></td> <td><input type="reset" name="reset"></td> </tr> <tr> <td> <div class="item"> <img src="contactus.jpg" width="254" height="54"> </div> </td> </tr> </table> </form> </body> 

Error: Have provided all the inputs correctly, but the ticket calculation is wrong or The javaScript method is not returning any boolean value

change form.tickets.value to form.getElementsByAttribute('name','tickets')[0].value

You should do the same to the rest of your form elements (showdate, childrens..)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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