menú de selección móvil permanecer en la opción de selección de URL actual

jQuery:

   $(function() {
            $('select').change(function(){
            var url = $(this).val();
            window.location = url;
            });
       });

HTML:

<select>
  <option value="http://www.coolwebsite.com.au/Sandbox/">Home</option>
  <option value="http://www.coolwebsite.com.au/Sandbox/kitchen.php">Kitchens</option>
  <option value="http://www.coolwebsite.com.au/Sandbox/fitouts.php">Shop Fitouts</option>
  <option value="http://www.coolwebsite.com.au/Sandbox/officefitouts.php">Office Fitouts</option>
  <option value="http://www.coolwebsite.com.au/Sandbox/Gallery/">Gallery</option>
  <option value="http://www.coolwebsite.com.au/Sandbox/joinery.php">Joinery</option>
  <option value="http://www.coolwebsite.com.au/Sandbox/jobs.php">Recent Jobs</option>
  <option value="http://www.coolwebsite.com.au/Sandbox/contact.php">Contact Us</option>
</select> 

It works just fine, but once the select option is clicked and new page appears, the select box returns to 'Home' option, rather than current page.

Wondering how to keep this option selected.

preguntado el 12 de febrero de 14 a las 08:02

Have I understood this right? You want to reload the page and the selected value on the new page should be set to what you selected from the previous? -

You will need to add the selected page as the selected value in the relevant php page. E.g. if you select Kitchens from home you will need to set the select in kitchen.php leer <option selected value="http://www.coolwebsite.com.au/Sandbox/kitchen.php">Kitchens</option> -

I am loading my mobile menu from a php include, the same php include throughout all pages so this solution wouldn't be optimal. -

2 Respuestas

Prueba esto:

$(function()
    var url = window.location.href;
    $('select option[value="'+url+'"]').prop("selected",true);
});

Respondido 12 Feb 14, 08:02

He's reloading the page. This will not work upon reload - paolo casciello

Since you're reloading the page, it's the code generating the page (in this case the PHP code) that must write the attribute selected al correspondiente <option> etiqueta.

<select>
  <option value="http://www.coolwebsite.com.au/Sandbox/">Home</option>
  <option value="http://www.coolwebsite.com.au/Sandbox/kitchen.php" selected>Kitchens</option>
  ...
</select> 

Respondido 12 Feb 14, 08:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.