0

我正在使用 jQuery。

$('#ranges').on('change', '.combo', function() {
        var selectedValue = $(this).val();
        var s = $(this).parent("div").attr("class");
        if ($(this).find('option').size() > 2) {
            var newComboBox = $(this).clone();
            var thisComboBoxIndex = parseInt($(this).attr('data-index'), 10);
            var newComboBoxIndex = thisComboBoxIndex + 1;

        $('div.'+s+' .parentCombo' + thisComboBoxIndex).remove();

        if (selectedValue !== '') {
            newComboBox.attr('data-index', newComboBoxIndex);
            newComboBox.attr('id', 'combo' + newComboBoxIndex);
            newComboBox.addClass('parentCombo' + thisComboBoxIndex);
            newComboBox.find('option[val="' + selectedValue + '"]').remove();
            $('div.'+s).append(newComboBox);
        }

});

小提琴

我如何保证之前的价值总是高于下一个?可能会提醒用户该问题或将该 combobox_text_value 变为“红色”或阻止用户提交。

在小提琴例如。如果我在第一个组合中选择 2,我应该只在下一个组合中选择 3。

ps:如果您需要更多代码,请注意我。

使用值处理动态组合框

4

1 回答 1

0

请参阅工作 jsfiddle:

http://jsfiddle.net/JaVVe/22/

$('body').on('change', '.combo', function() {
    var selectedValue = $(this).val();

    if ($(this).find('option').size() > 2) {
        var $newComboBox = $(this).clone();
        var thisComboBoxIndex = parseInt($(this).data('index'), 10);
        var newComboBoxIndex = thisComboBoxIndex + 1;

        $('.parentCombo' + thisComboBoxIndex).remove();

        if (selectedValue !== '') {
            $newComboBox.data('index', newComboBoxIndex)
                        .attr('id', 'combo' + newComboBoxIndex)
                        .addClass('parentCombo' + thisComboBoxIndex)
                        .find('option')
            .filter(function(){return this.value<=selectedValue && this.value}).remove();
            if($('option',$newComboBox).size() > 1)
               $('body').append($newComboBox);
        }
    } 

});
于 2013-01-09T15:48:44.290 回答