Jump to content

Function calculate problem #2


Skipjackrick

Recommended Posts

First off, thanks mainewoods for finding my mispelling in my last code.  Now, I am trying to calculate the total points with a different value for each species of fish on the form.

 

I thought I could use an "if" statement that would provide the correct value to multiply by.  But it seems it isn't so easy. 

 

My code is shown below.  How do I apply a different value for the var species1 in my function. 

 

For example if "species"=1 then {var species1=10}  I would hope??????

 

<html>
<head>
<title>Kayak Wars Submission Form</title>
<script type="text/javascript">
function calc_points()
{
if (getElementById("species").value=0) {var species1 = 0};
if (getElementById("species").value=1) {var species1 = 10};
if (getElementById("species").value=2) {var species1 = 10};
if (getElementById("species").value=3) {var species1 = 20};
if (getElementById("species").value=4) {var species1 = 20};
if (getElementById("species").value=5) {var species1 = 10};
if (getElementById("species").value=6) {var species1 = 20};
if (getElementById("species").value=7) {var species1 = 40};
if (getElementById("species").value= {var species1 = 20};
if (getElementById("species").value=9) {var species1 = 40};
if (getElementById("species").value=10) {var species1 = 10};
if (getElementById("species").value=11) {var species1 = 50};
if (getElementById("species").value=12) {var species1 = 20};
if (getElementById("species").value=13) {var species1 = 50};
if (getElementById("species").value=14) {var species1 = 50};
if (getElementById("species").value=15) {var species1 = 100};
if (getElementById("species").value=16) {var species1 = 50};
if (getElementById("species").value=17) {var species1 = 100};
if (getElementById("species").value=18) {var species1 = 100};
if (getElementById("species").value=19) {var species1 = 100};
if (getElementById("species").value=20) {var species1 = 200};
var quantity1 = document.getElementById("quantity").value;
var total_points = (species1 * quantity1);
document.getElementById("points").value = total_points;
}
</script>
</head>
<body>
<form id="form1" name="form1" method="POST" action="">
    <input type="hidden" name="submit_id" value="NULL">
        <select name="species" id="species">
        <option value="0">Select Species</option>
        <option value="1">Red Drum</option>
        <option value="2">Trout</option>
        <option value="3">Snook</option>
        <option value="4">Shark</option>
        <option value="5">Black Drum</option>
        <option value="6">Snapper</option>
        <option value="7">King Mackerel</option>
        <option value="8">Grouper</option>
        <option value="9">Cobia (Ling)</option>
        <option value="10">Flounder</option>
        <option value="11">Tarpon</option>
        <option value="12">Tripletail</option>
        <option value="13">Permit</option>
        <option value="14">Blackfin Tuna</option>
        <option value="15">Yellowfin Tuna</option>
        <option value="16">Barracuda</option>
        <option value="17">Bonefish</option>
        <option value="18">Dorado</option>
        <option value="19">Wahoo</option>
        <option value="20">Billfish</option>
          </select>
        <span class="style2">
        <label>Quantity</label>
        </span>
        <label>
      <select name="quantity" id="quantity" onChange="calc_points();">
  <option value="0">Select Quantity</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
	  <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
          </select>

        </label>
  <span class="style2">
        <label>Total Points</label>
        </span>
        <label>
          <input name="points" id="points">

</body>
</html> 

Link to comment
Share on other sites

We, first make sure you use parseInt for the value so JavaScript covert it from a string into a integer.

 

Here is an example using switch (but you can use if as well)

<html>
<head>
<title>Kayak Wars Submission Form</title>
<script type="text/javascript">
function calc_points(){
var val = parseInt(document.getElementById("species").value);
var species1 = 0;
switch (val) {
	case 0:
		species1 = 0;
		break;
	case 1:
	case 2:
	case 5:
	case 10:
		species1 = 10;
		break;
	case 3:
	case 4:
	case 6:
	case 8:
	case 12:
		species1 = 20;
		break;
	case 7:
	case 9:
		species1 = 40;
		break;
	case 11:
	case 13:
	case 14:
	case 16:
		species1 = 50;
		break;
	case 15:
	case 17:
	case 18:
	case 19:
		species1 = 100;
		break;
	case 20:
		species1 = 200;
		break;
	default:
		species1 = 0;
}

var total_points = (species1 * parseInt(document.getElementById('quantity').value));

document.getElementById("points").value = total_points;
}
</script>
</head>
<body>
<form id="form1" name="form1" method="POST" action="">
    <input type="hidden" name="submit_id" value="NULL">
        <select name="species" id="species">
        <option value="0">Select Species</option>
        <option value="1">Red Drum</option>
        <option value="2">Trout</option>
        <option value="3">Snook</option>
        <option value="4">Shark</option>
        <option value="5">Black Drum</option>
        <option value="6">Snapper</option>
        <option value="7">King Mackerel</option>
        <option value="8">Grouper</option>
        <option value="9">Cobia (Ling)</option>
        <option value="10">Flounder</option>
        <option value="11">Tarpon</option>
        <option value="12">Tripletail</option>
        <option value="13">Permit</option>
        <option value="14">Blackfin Tuna</option>
        <option value="15">Yellowfin Tuna</option>
        <option value="16">Barracuda</option>
        <option value="17">Bonefish</option>
        <option value="18">Dorado</option>
        <option value="19">Wahoo</option>
        <option value="20">Billfish</option>
          </select>
        <span class="style2">
        <label>Quantity</label>
        </span>
        <label>
      <select name="quantity" id="quantity" onChange="calc_points();">
  <option value="0">Select Quantity</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
	  <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
          </select>

        </label>
  <span class="style2">
        <label>Total Points</label>
        </span>
        <label>
          <input name="points" id="points">

</body>
</html> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.