From a table row with two dropdowns, get selected value & which drop-down changed

后端 未结 2 478
Happy的楠姐
Happy的楠姐 2021-01-07 02:23

I am trying to update two cascading drop down inside a table there are many answers on SO for drop downs, but I was unable to find help on cascading updates

2条回答
  •  甜味超标
    2021-01-07 03:02

    You need to give both your .....

    Then your script will be

    $('#table').on('change', '.country', function() {
        var selectedValue = $(this).val();
        var row = $(this).closest('tr'); // get the row
        var stateSelect = row.find('.state'); // get the other select in the same row
        // make you ajax call passing the selectedValue to your controller
        // in the success callback, update the options of stateSelect 
        $.ajax({
            url: ...
            data { id: selectedValue },
            ....
            success: function(data) {
                stateSelect.empty();
                $.each(data, function(item, index) {
                    stateSelect.append($('').val(iem.ID).text(item.Name));
                }
            }
        });
    }
    

    Refer also better way to load 2 dropdown in mvc for details of the code for populating cascading dropdownlists (consider caching the options as per the 2nd code example to avoid repeated ajax calls)

提交回复
热议问题