Clean old options from child dropdown when receiving data by JSON

前端 未结 1 1890
-上瘾入骨i
-上瘾入骨i 2020-12-21 12:43

I populate child dropdown (second dropdown) by the onchange event in parent dropdown (first dropdown). After that by onchange event of child dropdown I am autofilling three

相关标签:
1条回答
  • 2020-12-21 13:19

    In auto.jsp '#combo' change anonymous function, replace:

    $.getJSON('combo1.jsp', {firstcombobox : this.value}, function(responseData) {
        $("#combo1").append(
            $("<option></option>").html(responseData.name).val(responseData.name)
        );
    });
    

    with:

    $.getJSON('combo1.jsp', {firstcombobox : this.value}, function(responseData) {
        $("#combo1").empty().append(
            $("<option></option>").html(responseData.name).val(responseData.name)
        );
    });
    

    To split the string into an array look here: How do I split a string, breaking at a particular character?

    They use as follows:

    $.getJSON('combo1.jsp', {firstcombobox : this.value}, function(responseData) {
        var splitValues = responseData.name.split(/,/);
    
        $("#combo1").empty().append("<option value="0">Please select...</option>");
    
        for (var idx in splitValues) {
            $("#combo1").append(
                $("<option></option>").html(splitValues[idx]).val(splitValues[idx])
            );
        }
    });
    

    Hope this helps?

    0 讨论(0)
提交回复
热议问题