Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 循环遍历列中的所有单元格,并用唯一项填充下拉列表_Jquery_Unique - Fatal编程技术网

Jquery 循环遍历列中的所有单元格,并用唯一项填充下拉列表

Jquery 循环遍历列中的所有单元格,并用唯一项填充下拉列表,jquery,unique,Jquery,Unique,我有一个筛选表的下拉列表。我希望在每次筛选表时重新填充下拉列表,以仅显示剩余的唯一项。目前我有以下功能 function rePopulateSelectList(column, control) { //wipe the previous drop down $('#' + control).find('option').remove(); // $('#adminTable tr').each(function () { $.unique($(thi

我有一个筛选表的下拉列表。我希望在每次筛选表时重新填充下拉列表,以仅显示剩余的唯一项。目前我有以下功能

    function rePopulateSelectList(column, control) {
//wipe the previous drop down
    $('#' + control).find('option').remove();
//
    $('#adminTable tr').each(function () {
         $.unique($(this).find('td:eq(' + column + ')')).each(function () {
            var columnText = $(this).text();
            $('#' + control).append('<option value="' + columnText + '">' + columnText + '</option>');
        });
    });
}

下拉列表正在select.change事件中重新填充,但我得到的下拉选项至少是原来的两倍,没有一个是唯一的。

请尝试下面的代码。$。unique不会通过比较td中的文本来为您提供唯一的tr

function rePopulateSelectList(column, control) {

    var $control = $('#' + control);

    //wipe the previous drop down
    $control.find('option').remove();

    $('#adminTable tr').each(function () {

        var columnText = $(this).find('td:eq(' + column + ')').text();

        if ($control.find("option[value='" + columnText + "']").length > 0) {
            return;
        }

        $control.append('<option value="' + columnText + '">' + columnText + '</option>');

    });
}

您的代码中似乎有一些问题。传入列索引假定只需要一列的值?试着这样做:

function rePopulateSelectList(columnIndex, select) {
    $('#' + select + ' option').remove();

    var control = $('#' + select), values = [];
    $('#adminTable tr').each(function () {
        var cell = $(this).find('td:eq(' + columnIndex + ')'),
            text = cell.text();

        if (values.indexOf(text) == -1) {
            values.push(text);
            control.append('<option value="' + text + '">' + text + '</option>');
        }
    });
}

您所指的唯一项是什么?我试图从我的表中获取唯一td的集合,即仅通过比较文本属性。我现在假设唯一的比较不起作用,因为每个td都在一个不同的trHow中,你可以在中看到。顺便说一下,'td:eq'+列+返回单个td。使用find'td:eq'+column+是没有意义的。每个…+1现在对我来说都很好,即使在你的位置上,我会将$control.append放在里面,如果将>0更改为==0,当然是为了使代码更短更容易。反转条件有助于降低嵌套级别和使用习惯-+这对我来说也很合适。我要简化的一件事是:单元格只用于获取文本。因此可以简化为var text=$this.find'td:eq'+columnIndex+.text;是的,你说得对。我加入“cell”只是为了明确代码在做什么。这看起来更像,indexOftext==-1是我需要的黄金。非常感谢