【问题标题】:On Select change Disable option if selected previois and create a clone row not workingOn Select change Disable option if selected previois and create a clone row not working
【发布时间】:2015-09-14 22:04:47
【问题描述】:

如果在前一行被选中,我试图禁用选择选项说如果最后一行文本框的值被填充并且选择被更改,我的 html 表会创建一行我试图做的是如果选择选项在创建新行之前被选中,当我添加禁用代码时应该禁用选择选项,然后不会创建新行。

Demo Fiddle with select code

Demo Fiddle with out select code

$('#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="test1">test1 </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>');

//*******the select code if this is removed all works fine******?//
setTimeout(function () {
    var $selects = $('select');
    $selects.on('change', function () {


        $("option", $selects).prop("disabled", false);
        $selects.each(function () {
            var $select = $(this),
                $options = $selects.not($select).find('option'),
                selectedText = $select.children('option:selected').text();
            $options.each(function () {
                if ($(this).text() == selectedText) $(this).prop("disabled", true);
            });
        });
    });

    $selects.eq(0).trigger('change');

}, 99);



$('#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.next('tr').remove();
            tr.data('done', false);
        }
    }
});

$('#buttonclck').on('click', function () {
    var lastRow = $('#productanddates').closest('#productanddates').find("tr:last-child");

    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);
});


$('#results').on('change', '.dd', function (e) {
    var data = "dummy data";
    $(this).closest('td').prev().find('input').val(data).trigger('input');
    $(this).closest('td').next().find('input').val(data).trigger('input');
});

【问题讨论】:

  • 目标是禁用在前几行中选择的option 元素吗?当 select 的所有值/选项都用完时会发生什么?
  • 这将是没有更多选择的结尾,最后一个空白行将留空
  • 被告知将不会再自动增加所有选择的行被选中

标签: javascript jquery


【解决方案1】:

以下代码在添加新行后执行,应遍历先前的行并禁用 select 的新行值已使用:

    var prevRows = cloned.siblings();
    cloned.find('select option').each(function(index,option) {
        prevRows.each(function(i,tr) {
            option.value !== $('select', tr).val() || $(option).prop('disabled',  true);
        });
    }); 

演示

【讨论】:

  • 完美,但我不明白你为什么要添加多个演示
  • 只是向您展示了做同样事情的其他方法,但第一种是最优化的。
  • 当然谢谢我有一个小问题,说你在第一行选择了 test1 并填充了所有数据,然后在创建的新行上 test1 被禁用,当你转到第一行并更改为test2,因为我在第二行选择了 test2,所以两者都是一样的
  • 还有一个问题是如果我选择 test2 那么它应该在第一行被禁用
  • 我提供的代码 sn-p 在新行上运行,遍历其他行以禁用已选择的选项。我相信您可以添加类似的代码来禁用除新行之外的行中的选项。试一试:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-28
  • 2017-03-03
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多