最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Take input from HTML form into Javascript? - Stack Overflow

programmeradmin1浏览0评论

I am trying to make a website that calculates a delivery based on zip code. Is the best way to get data from the HTML form into JavaScript? I want it to do take the user's zip code and alert the user what the calculated fee would be. Can somebody provide some insight on how to do this. Here's my code:

<h3><i>Begin by entering your requested order below</i><h3>
<form action="#" method="post" id="delInfo">
	Zip Code:<br>
	<input type="text" name="zipcode" id="zipcode"><br>
	<p><i>Delivery fee will be calculated based on zip code</i><p>
	First Name:<br>
	<input type="text" name="firstname" id="firstname"></br>
	Last Name:<br>
	<input type="text" name="streetaddress" id="lastname"></br>
	Street Address:<br>
	<input type="text" name="zipcode" id="address"></br>
	Email Address:<br>
	<input type="text" name="email"></br>
	<input type="submit" value="Submit">
	
	
</form>
<script>
	function newDel() {
		'use strict';
		var zip = document.getElementById('zipcode');
		var firstname = document.getElementById('firstname');
		var lastname = document.getElementById('lastname');
		var street address = document.getElementById('address');
		var email = document.getElementById('emailaddress');
	
	
	}
	function zipCalc(zip) {
		if zip == 32231{
		alert("your fee added delivery fee will be 2 dollars.")
		}
		
	}

</body>
</html>

I am trying to make a website that calculates a delivery based on zip code. Is the best way to get data from the HTML form into JavaScript? I want it to do take the user's zip code and alert the user what the calculated fee would be. Can somebody provide some insight on how to do this. Here's my code:

<h3><i>Begin by entering your requested order below</i><h3>
<form action="#" method="post" id="delInfo">
	Zip Code:<br>
	<input type="text" name="zipcode" id="zipcode"><br>
	<p><i>Delivery fee will be calculated based on zip code</i><p>
	First Name:<br>
	<input type="text" name="firstname" id="firstname"></br>
	Last Name:<br>
	<input type="text" name="streetaddress" id="lastname"></br>
	Street Address:<br>
	<input type="text" name="zipcode" id="address"></br>
	Email Address:<br>
	<input type="text" name="email"></br>
	<input type="submit" value="Submit">
	
	
</form>
<script>
	function newDel() {
		'use strict';
		var zip = document.getElementById('zipcode');
		var firstname = document.getElementById('firstname');
		var lastname = document.getElementById('lastname');
		var street address = document.getElementById('address');
		var email = document.getElementById('emailaddress');
	
	
	}
	function zipCalc(zip) {
		if zip == 32231{
		alert("your fee added delivery fee will be 2 dollars.")
		}
		
	}

</body>
</html>

Share Improve this question asked Mar 2, 2017 at 15:31 zestyxylaphonezestyxylaphone 211 silver badge2 bronze badges 1
  • They use APIs that calculate it – epascarello Commented Mar 2, 2017 at 15:33
Add a ment  | 

3 Answers 3

Reset to default 2

As epascarello vaguely hinted, you would probably need to municate with the delivery pany's API in order to get realistic prices. If you're going to charge based on zip code as you've implied, then what you're working up should work. That said, let's talk JavaScript!


First, document.getElementById() returns the Document Object Model (DOM) element. You need to access its value property to get the value in the textbox, like this:

var zipElement = document.getElementById("zipcode");
var zipVal = zipElement.value;

You can do that in one line like this, if you don't need the reference to the element after that:

var zipVal = document.getElementById("zipcode").value;

if statements should have parentheses around the condition:

if (zipVal == 32231) {
    alert("Delivery fee will be 2 dollars.")
}

Now you need to add a listener to your button to call your JavaScript function. I moved your javascript above the form because the code has to exist before you assign the onClick function!

Add onClick="newDel(); To prevent the form from submitting if there is a validation error (say you want them to fill out Zip Code, and they don't), then you can return false; in the newDel() function.

    <script>
        /*I moved your javascript above the form because the code has to exist before you assign the onClick function!
        */
        function newDel() {
          var zipVal = document.getElementById('zipcode').value;
          var firstname = document.getElementById('firstname');
          var lastname = document.getElementById('lastname');
          var streetAddress = document.getElementById('address');
          var email = document.getElementById('emailaddress');	

          if(zipVal == ""){
              alert("Please provide a zip code.");
              return false;
          } else {
              //Now call your zipCalc() function:
              zipCalc(zipVal);
          }
        }
    
        function zipCalc(zip) {
          if (zip == 32231) {
              alert("Delivery fee will be 2 dollars.");
          }
        }
    </script>

    <form action="#" method="post" id="delInfo">
    	Zip Code:<br>
    	<input type="text" name="zipcode" id="zipcode"><br>
    	<p><i>Delivery fee will be calculated based on zip code</i><p>
    	First Name:<br>
    	<input type="text" name="firstname" id="firstname"></br>
    	Last Name:<br>
    	<input type="text" name="streetaddress" id="lastname"></br>
    	Street Address:<br>
    	<input type="text" name="zipcode" id="address"></br>
    	Email Address:<br>
    	<input type="text" name="email"></br>
    	<input type="submit" onClick="newDel()" value="Submit">
    </form>

(Side note: You can more easily work with DOM elements with JavaScript libraries like jQuery, which allows you to query for elements using their class, ID, element type, and many other selectors.)

I assume you want this calculation done before form submit so you will need to add an event listener to the zip input

document.addEventListener('DOMContentLoaded', function(){

  var zip            = document.getElementById('zipcode');
  // when the zip input value changes it will alert the user
  zip.addEventListener('change', zipCalc)

  var firstname      = document.getElementById('firstname');
  var lastname       = document.getElementById('lastname');
  var street_address = document.getElementById('address');
  var email          = document.getElementById('emailaddress');

  function zipCalc(e) {
    zip_input_val = zip.value;
    if (zip_input_val == 32231) {
        alert("your fee added delivery fee will be 2 dollars.")
    }
    else {
        alert('We cant calculate your cost');
    }
  }

})
<h3><i>Begin by entering your requested order below</i><h3>
<form action="#" method="post" id="delInfo">
	Zip Code:<br>
	<input type="text" name="zipcode" id="zipcode"><br>
	<p><i>Delivery fee will be calculated based on zip code</i><p>
	First Name:<br>
	<input type="text" name="firstname" id="firstname"><br/>
	Last Name:<br>
	<input type="text" name="streetaddress" id="lastname"><br/>
	Street Address:<br>
	<input type="text" name="zipcode" id="address"><br/>
	Email Address:<br>
	<input type="text" name="email"><br/>
	<input type="submit" value="Submit">
	
	
</form>

Js Fiddle

You are close, you just need the value after you getElementByID

You can use:

function zipCalc(zip) {
    var zip = document.getElementById("zipcode").value; // this gets value from the form element with ID zipcode.
    if (zip == "32231"){
    alert("your fee added delivery fee will be 2 dollars.")
    }

}
发布评论

评论列表(0)

  1. 暂无评论