簡體   English   中英

如果已選擇,則禁用選擇選項

[英]Disable Select Option if already selected

我創建了一個表,如果填充了所有文本框並且在前一行中更改了select則會克隆其最后一行。 我試圖添加一個條件,如果已經在之前的行中選擇了新的下拉選項,則應該禁用它們。

演示Fillde

禁用選項代碼:

$('select').change(function() {

    var value = $(this).val();

    $(this).siblings('select').children('option').each(function() {
        if ( $(this).val() === value ) {
            $(this).attr('disabled', true).siblings().removeAttr('disabled');   
        }
    });

});

完整的JS:

$('#results').append('<table width="100%" border="1" cellspacing="0" cellpadding="5" id="productanddates" class="border"> <tr><td> <input type="text" name="to1" id="to1" value="" /> </td> <td> <select class="dd" name="Phonenumberdd1" id="Phonenumberdd1"> <option value="test">test </option><option value="test2">test 2</option></select></td> <td>   <input type="text" name="renewal_by1" id="renewal_by1" />  </td>   <td> <input type="text" name="Renivaul_to1" id="Renivaul_to1" value="" /> </td></TR></TABLE>'
);
$('select').change(function() {

    var value = $(this).val();

    $(this).siblings('select').children('option').each(function() {
        if ( $(this).val() === value ) {
            $(this).attr('disabled', true).siblings().removeAttr('disabled');   
        }
    });

});
    $('#results').on('focus', ':input', function() {
        $(this).closest('tr').filter(function() { 
            return !$(this).data('saved'); 
        })
        .find(':input').each(function() {
            $(this).data('value', this.value);
            $(this).closest('tr').data('saved', true);
        });
    })
    .on('input change', ':input', function() {
        $(this).data('filled', this.value != $(this).data('value'))
        var tr  = $(this).closest('tr');
            all = tr.find(':input'),
            fld = all.filter(function() {
                return $(this).data('filled');
            });
        if( all.length == fld.length ) {
            if( !tr.data('done') ) {
                $('#buttonclck')[0].click();
                tr.data('done', true);
            }
        } else {
            if( tr.data('done') ) {

                tr.data('done', false);
            }
        }
    });

    $('#buttonclck').on('click', function () {
        var lastRow = $('#productanddates').closest('#productanddates').find("tr:last-child");
        var lastRowInputs = lastRow.find('input');
        var isClone = false;
        lastRowInputs.each(function() {
           if($(this).val().length) {
               isClone = true;
           }
        });
        if(!isClone)
            return false;
        var cloned = lastRow.clone();
        cloned.find('input, select').each(function () {
            var id = $(this).attr('id');

            var regIdMatch = /^(.+)(\d+)$/;
            var aIdParts = id.match(regIdMatch);
            var newId = aIdParts[1] + (parseInt(aIdParts[2], 10) + 1);

            $(this).attr('id', newId);
            $(this).attr('name', newId);
        });

        cloned.find("input[type='text']").val('');
        cloned.insertAfter(lastRow);
    });

HTML:

<div id="results"></div>

<input id="buttonclck" type="button" class="hide"  value="button"/>

你的錯誤在這里:

$(this).siblings('select').children('option').each(function() { ... }

它應該是 :

$(this).children('option').each(function() { ... }   

為什么選擇select元素的siblings 你應該直接下降到它的選擇。 我不知道是否有意,但這樣的代碼將在兩行中禁用option :previous和new generated。

這是小提琴:

http://jsfiddle.net/99pvwypp/19/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM