Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've looked at countless examples on here and not sure what I'm doing wrong. I'm trying to change the form action based on the selected value from a dropdown menu.

Basically, the HTML looks like this:

<form id="storetable" autocomplete="off" action="" method="post">
<label>STORE:</label>
  <input type="text" list="storeID" name="store" placeholder="Choose a store..." required>
       <datalist id="storeID">
          <option value="cwb_coins">Causeway Bay</option>
          <option value="wc_coins">Wan Chai</option>
          <option value="lck_coins">Lai Chi Kok</option>
          <option value="tp_coins">Tai Po</option>
       </datalist>
</form


What I have tried:

<script>
    $(document).ready(function(){
    $("#storeID").change(function(){
   var url =  $(this).children(":selected").text(); //get the selected option value
   switch (url) 
  {
   case "cwb_coins":
   $("#storetable").attr('action','cwb_coin_code.php');
   //changing action attribute of form to cwb_coin_code.php
   break;
   case "wc_coins":
   $("#storetable").attr('action','wc_coin_code.php');
   break;
   case "lck_coins":
   $("#storetable").attr('action','lck_coin_code.php');
   break;
   case "tp_coins":
   $("#storetable").attr('action','tp_coin_code.php');
   break;
   default:
       $("#storetable").attr('action', '#');
   }
   }); 
 }); 
</script>
Posted
Updated 1-Jul-19 22:25pm

1 solution

You need to learn to use the debugger, you will never fix problems by simply looking at the code. First thing, does your change event actually fire? Put a breakpoint inside it or the "debugger" statement. You are attaching to the change even of the datalist which is probably wrong, you probably need to attach to the change event of the "input" element.

Once that event is firing again use breakpoints etc to examine the contents of "url". You might find it doesn't contain the text you think it does, you probably want to get the ".val()" rather than ".text()".
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900