#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Using switch statement to get value for drop down list not working plz help


    i want to get the value when selecting from drop down i have used switch but it is not working for second case when selecting from drop down.....plz help


    <html>

    <head>


    <script type="text/javascript">
    function Calculation()
    {
    var vtype=document.getElementById('vtype').value;
    var pickup=document.getElementById('pickup').value;
    var returnnow=document.getElementById('returnnow').value;


    if(vtype="Indica/Alto (4 seater)" && pickup=="Kathgodam")

    {
    switch(returnnow)
    {
    case "Nainital":
    cost=800;
    break;
    case "Pantnagar":
    cost=700;
    break;


    }
    }

    document.getElementById("TotalCost").value=cost;


    if(vtype="Indica/Alto A/c (4 seater)" && pickup=="Kathgodam")

    {
    switch(returnnow)
    {
    case "Nainital":
    cost=1100;
    break;
    case "Pantnagar":
    cost=1000;
    break;


    }

    }
    }

    </script>

    </head>
    <body style="background-color: #FFFFFF; topmargin="0"; text-align:left">

    <form method="post" action="?">
    <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
    <tr>
    <table border="0" cellspacing="0" cellpadding="1"><tr>
    <td valign="top">
    <table border="0" cellspacing="0" cellpadding="2" class="reztbl" style="border-collapse: collapse" bordercolor="#111111"><tr>
    <th colspan="2" class="tblhead1" valign="top">Pickup- Drop off Rate Calculator</th></tr>
    <tr><td class="tbldet1" style="text-align:right" valign="top">Vehicle Type</td>
    <td class="tbldet1" valign="top">
    <select name="vehicleType" id="vtype" >
    <option>Indica/Alto (4 seater)</option>
    <option>Indica/Alto A/c (4 seater)</option>


    </select>
    </td></tr>
    <tr><td class="tbldet1" style="text-align:right" valign="top">Pick-Up Location</td>
    <td class="tbldet1" valign="top"><select name="Pickup" id="pickup" >
    <option value="XX">Pick-Up Location</option>
    <option value="Kathgodam">Kathgodam</option>

    </select></td></tr>
    <tr><td class="tbldet1" style="text-align:right" valign="top">Drop-Off Location</td>
    <td class="tbldet1" valign="top"><select name="Return" id="returnnow" >
    <option value="XX">Drop-Off Location</option>

    <option value="Pantnagar">Pantnagar</option>
    <option value="Nainital">Nainital</option>





    </select></td></tr>
    <tr><td class="tbldet1" style="text-align:right" valign="top">Other Requirements </td>
    <td class="tbldet1" valign="top"><textarea cols="30" rows="5" name="Special"></textarea></td></tr>
    <tr><th class="tblhead2" colspan="2" valign="top">
    <input type="button" class="btn" name="CalcBtn" value="Calculate"
    onClick="return Calculation();">&nbsp;&nbsp;&nbsp;&nbsp; </th></tr>
    <tr><th class="tblhead2" style="text-align:right" valign="top">Total Amount
    Rs.</th>
    <th class="tblhead2" style="text-align:left;" valign="top"><input type="text" id="TotalCost" name="TotalCost" value="" size="10" class="txtfld"></th></tr>

    </table></td></tr></table> </tr>
    </table>

    </form>
    </body>
    </html>
  2. #2
  3. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Code:
    if(vtype=="Indica/Alto (4 seater)" && pickup=="Kathgodam")
    twice
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by AndrewSW
    Code:
    if(vtype=="Indica/Alto (4 seater)" && pickup=="Kathgodam")
    twice


    this is not working i had tried it already......
  6. #4
  7. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,008
    Rep Power
    2790
    AndrewSW has posted the correct solution. You need to change both of the instances of an assignment operator to a comparison operator, not just the first one.
    Last edited by Winters; September 6th, 2012 at 11:39 AM. Reason: clarity
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  8. #5
  9. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Code:
    document.getElementById("TotalCost").value=cost;
    .. and the above line is used between the two switch statements. So it will be called once and the second switch won't achieve anything.

    There are a number of errors in your HTML as well. Validate

    I recommend learning CSS and avoiding nested tables.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by AndrewSW
    Code:
    document.getElementById("TotalCost").value=cost;
    .. and the above line is used between the two switch statements. So it will be called once and the second switch won't achieve anything.

    There are a number of errors in your HTML as well. Validate

    I recommend learning CSS and avoiding nested tables.
    hi plz post the full corrected code as i need this for my brother he in new to progmng and he had to submit this to cllge ..and i am a mechanical guy and he ask me for this to post in fourm site so plz post full corrected code....
  12. #7
  13. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Erm, no, not really! If your brother is not able to complete the assignment for himself then he needs to talk to his teacher.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by AndrewSW
    Erm, no, not really! If your brother is not able to complete the assignment for himself then he needs to talk to his teacher.
    plz help teacher here are not helping the student thats why i had posted it in this fourm...so plz help this time

IMN logo majestic logo threadwatch logo seochat tools logo