1
Answer

How to change form action attribute value based on selection

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:
  1. <form id="storetable" autocomplete="off" action="" method="post">  
  2. <label>STORE:</label>  
  3. <input type="text" list="storeID" name="store" placeholder="Choose a store..." required>  
  4. <datalist id="storeID">  
  5. <option value="cwb_coins">Causeway Bay</option>  
  6. <option value="wc_coins">Wan Chai</option>  
  7. <option value="lck_coins">Lai Chi Kok</option>  
  8. <option value="tp_coins">Tai Po</option>  
  9. </datalist>  
  10. </form  
  1. <script>    
  2.  $(document).ready(function(){    
  3.   $("#storeID").change(function(){    
  4.   var url =  $(this).children(":selected").text(); //get the selected option value    
  5.   switch (url)     
  6.  {    
  7.   case "cwb_coins":    
  8.   $("#storetable").attr('action','cwb_coin_code.php');    
  9.   //changing action attribute of form to school.php    
  10.   break;    
  11.   case "wc_coins":    
  12.   $("#storetable").attr('action','wc_coin_code.php');    
  13.   break;    
  14.   case "lck_coins":    
  15.   $("#storetable").attr('action','lck_coin_code.php');    
  16.   break;    
  17.   case "tp_coins":    
  18.   $("#storetable").attr('action','tp_coin_code.php');    
  19.   break;    
  20.   default:    
  21.       $("#storetable").attr('action''#');    
  22.   }    
  23.   });     
  24. });     
  25. </script>  

Answers (1)