JQuery: re-enable selected and disabled option

I am trying to re-enable the selected and disabled option from the drop-down list as soon as they are removed from the added list. Can someone help me please. Thanks in advance. The following is what I have so far, and here is the http://jsfiddle.net/N2jyy/2/ demo page.

$(document).ready(function() {
    $('#Add-btn').live('click',function() {
        $(document.body).append($("<div>"));
            var a = $('#a').val();
            $("#tabName tr:first").before("<tr><td class='greyed'><input type='hidden' name='added_a' value='" + a + "'>" + a + "</td><td style='background:transparent;'><input type='button' class='delete' value='Remove'></td></tr>");
            $("#a option:selected").attr("disabled", true);
    });
    $('#tabName td .delete').live('click',function() {
        // here I want to re-enable the select option ...removeAttr('disabled');
        $(this).parent().parent().remove(); 
        return false;
    });
});​
+3
source share
2 answers

You can do it as follows:

$('#tabName td .delete').live('click',function() {
    var val = $(this).closest('tr').remove().find('input').val();
    $("#a option[value='" + val + "']").attr("disabled", false);
    return false;
});

Try here what it does up to level <tr>, and then up <input>, while deleting at the same time <tr>, we use this value to select the option that you want to re-enable.

+3
source

, :

$(document).ready(function() {
    $('#Add-btn').live('click', function() {
        $(document.body).append($("<div>"));
        var a = $('#a').val();
        $("#tabName tr:first").before("<tr><td class='greyed'><input type='hidden' name='added_a' value='" + a + "'>" + a + "</td><td style='background:transparent;'><input type='button' class='delete' value='Remove'></td></tr>");
        $("#a option:selected").attr("disabled", true);
    });
    $('#tabName td .delete').live('click', function() {
        var delindex = $(this).parent().prev().find('input').val() - 1;
        $("#a option:eq(" + delindex + ")").removeAttr("disabled");
        $(this).parent().parent().remove();
        return false;
    });
});
0

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


All Articles