1

On our home page we have a drop down menu that takes you to a country specific page.

We are using the the jquery change function to point the user to the correct country when the country is selected in a dropdown box.

If the user selects a country and and presses back after viewing the country page, and then wishes to view the same country page again, they have to select another country, press back and then select the previous desired country. Any way around this?

$(document).ready(function() {
$('select.jumpmenu').change(function(){ 
        if($(this).find("option:selected").val() != 'no_selection'){
            $.ajax({
                    type: "GET",
                    url: "/countries/jsontariff/" + $(this).find("option:selected").val(),
                    success: function(html){
                        window.location = "/recommend/";
                    }
            });
        }       
    });
});
4
  • Can you post some code samples? Commented Apr 13, 2011 at 15:19
  • How about executing the same code on document ready that you execute on the change event? Commented Apr 13, 2011 at 15:23
  • hey, it is within document.ready Commented Apr 13, 2011 at 15:28
  • Nope, binding the change event is in document.ready :) It's not the same thing. Check ohmusama's answer below, that is what I was driving at. Commented Apr 13, 2011 at 15:43

2 Answers 2

1

Try to reset the select onload:

$(document).ready(function(){$('#selectList option:first').attr('selected',true)})
Sign up to request clarification or add additional context in comments.

Comments

0

I would execute the same code on page load.

executeCountrySelection() {
    if($('select.jumpmenu').find("option:selected").val() != 'no_selection'){
        $.ajax({
                type: "GET",
                url: "/countries/jsontariff/" + $(this).find("option:selected").val(),
                success: function(html){
                    window.location = "/recommend/";
                }
        });
    }
}

$(document).ready(function() {

    //executes when the page loads
    executeCountrySelection();

    //then again when the select is changed
    $('select.jumpmenu').change(function() { executeCountrySelection(); });

});

1 Comment

You could also manually trigger the change event after assigning the change function, but I like your way better, as it seems a little more clear and forthright.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.