views:

34

answers:

2

Hi.

I have this select list:

<select url="/Admin/SubCategories" name="TopParentId" id="ParentList">
  <option value="">Please select a parent category</option>
  <option value="1" selected="selected">New Store</option>
  <option value="2">Extensions</option>
  <option value="3">Remodel</option>
  <option value="4">Bespoke Signage</option>
  <option value="5">Tactical Signage</option>
  <option value="6">Size Chart</option>
  <option value="7">Contact Info</option>
</select>

As you can see the option 1 is marked as selected. When I change the selection, I use this code to do an ajax call to get some values to populate a new select list:

$("#ParentList").unbind("change");
$("#ParentList").change(function() {
    var itemId = $(this).val();
    var url = $(this).attr("url");
    var options;

    $.getJSON(url, itemId, function(data) {
        var defaultoption = '<option value="0">Please select a sub-category</option>';
        options += defaultoption;

        $.each(data, function(index, optionData) {
            var option = '<option value="' + optionData.valueOf + '">' + optionData.Text + '</option>';

            options += option;
        });

        $("#SubParentList").html(options);
    });        
});

My problem is that whenever I change the selection, the itemId is always the id of option 1, because it is marked as selected. It doesn't pick up the value of the option it is being changed too.

Can someone please enlighten me of their knowledge.

Regards,

Jean-Philippe

A: 

I think this is what you need -- get the selected option value, try

var itemId = $(this).find('option:selected').val();
Kerry
I have tried this too, but it still selects the item that was originally selected. The problem is that it doesn't change the selection on change.
Jean-Philippe
What do you mean it doesn't change the selection on change?
Kerry
I mean that the item that was originally marked as selected is still marked as selected, therefore the itemid is always that value.
Jean-Philippe
I agree with KP below, problem lies elsewhere, is there any other code affecting it anywhere?
Kerry
A: 

I believe your problem lies elsewhere. I just tested your code (excluding the JSON) and itemId is being set to the selected item as expected.

var itemId = $(this).val(); is getting the correct value on change.

Note that I'm assuming you have wrapped your js code above in $(document).ready(function(){});

Working example of getting value: http://jsfiddle.net/aqHrc/

KP
Yes you are right. Except that it still returns the value of the originally selected item. There might be something happening on databinding of the select list.
Jean-Philippe
In your JSON request `$.getJSON(url, itemId,....` you are passing the correct id to the server. My code example shows this. Your error may be elsewhere in your code IMO. Try manually setting itemId to a different value before the JSON request. My suspicion is the 'error' will still happen. I think you may have a different problem.
KP