JQuery SelectList parameter Modified is not updated

I have this selection 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, option 1 is marked as selected. When I change the selection, I use this code to call ajax to get some values ​​to populate the new selection 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, itemId is always the identifier of option 1, because it is marked as selected. It does not receive the value of the parameter, which it also changes.

Can someone please enlighten me about their knowledge.

Hi,

Jean philip

+3
source share
3 answers

I had the same problem and found a nice workaround:

var before= $('#selectmenu').html();
$('#selectmenu').html("");
$('#selectmenu').html(before);

If you do, the selection box should be updated.

+3
source

, , - ,

var itemId = $(this).find('option:selected').val();
+1

, . ( JSON) itemId , .

var itemId = $(this).val(); gets the correct value when changing.

Please note that I assume you wrapped the js code above in $(document).ready(function(){});

A working example of getting a value: http://jsfiddle.net/aqHrc/

+1
source

Source: https://habr.com/ru/post/1749092/


All Articles