tags:

views:

333

answers:

3

I want to preset the value of a selectbox based on a hidden field. I need this after an error has been detected in a form to avoid the user having to set the value themselves again. I do this by setting the value of a hidden field server side. The problem I have seems to be that the select box isn't done yet at the time I try to set the selected value. Anyone know how to solve this (possibly in a very different way?)

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"&gt;&lt;/script&gt;

<script type="text/javascript" charset="utf-8">
$(function(){
    // this functions loads the state select box with states if a country with states is selected
$("select#countries").change(function(){
  $.getJSON("/ajax/exhibition/getstates.php?idCountry="+$("select#countries").val(), function(j){
        var options = '';
        $.each(j, function(key, value){
            options += '<option value="' + key + '">' + value + '</option>';
        });
    $("select#state").html(options);
    });
});

});
$(document).ready(function(){
// preset the state select box on page ready
$('select#countries').change();

// set the selected value of the state select box
var foo = $('#statepreset').val();
$("select#state option[value="+foo+"]").attr('selected', 'selected');


});
</script>
A: 

You could add the preselect-code to the callback of the getJson

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"&gt;&lt;/script&gt;

<script type="text/javascript" charset="utf-8">
$(function(){
    // this functions loads the state select box with states if a country with states is selected
$("select#countries").change(function(){
  $.getJSON("/ajax/exhibition/getstates.php?idCountry="+$("select#countries").val(), function(j){
        var options = '';
        $.each(j, function(key, value){
            options += '<option value="' + key + '">' + value + '</option>';
        });
        $("select#state").html(options);
        var foo = $('#statepreset').val();
        $("select#state option[value="+foo+"]").attr('selected', 'selected');
    });
});
});
harpax
ok, this seems to work, but this is also called on every change of country and thus will try to reset the state list's selected option to the one from the hidden field upon every country change. I'd like it to be only executed on page load
Maarten
+1  A: 

I would suggest this approach:

function initSelect(defaultVal) {
    $("select#countries").change(function(){
        $.getJSON("/ajax/exhibition/getstates.php?idCountry="+$("select#countries").val(),     function(j){
            var options = '';
            $.each(j, function(key, value){
                options += '<option value="' + key + '">' + value + '</option>';
            });
            $("select#state").html(options);
            $("select#state option[value="+defaultVal+"]").attr('selected', 'selected');
        });
    }).change();
}


$(document).ready(function(){
    initSelect($('#statepreset').val());
});
karim79
used this code, seems a nice cleaner rewrite of mine, but see my comment for the answer from harpax. I consider it a small problem, but I feel like I should go about solving the whole problem in a different way altogether
Maarten
+1  A: 

Hey!

The quick fix for this is to put the code that modifies the selection of the state, inside the callback (inside the getJSON callback)

$("select#countries").change(function(){
  $.getJSON("/ajax/exhibition/getstates.php?idCountry="+$("select#countries").val(), function(j){
        var options = '';
        $.each(j, function(key, value){
            options += '<option value="' + key + '">' + value + '</option>';
        });
    $("select#state").html(options);

    // set the selected value of the state select box
    var foo = $('#statepreset').val();
    $("select#state option[value="+foo+"]").attr('selected', 'selected');
    });
});

Remember that the callback you supply to the getJson call is only executed once the server replies.

JT