如何在下拉菜单中添加标签,如果选择选项来自DataTables JavaScript?

huangapple go评论85阅读模式
英文:

How to add labels to dropdown menu if the select options are coming from datatables javascript?

问题

我正在使用一个带有下拉筛选器的数据表格进行项目,其中我正在动态从数据表格中填充下拉菜单。自动化的508合规性工具显示了以下错误,针对3个下拉菜单:“没有可访问名称的元素:(3)”。它还提供了以下网站的链接,说明如何修复此错误。https://www.ssa.gov/accessibility/andi/help/alerts.html?no_name_form_element。

我不确定如何添加标签,因为我没有在HTML中定义选择选项。它是来自JavaScript的。请参见下面的代码。任何指导都将非常有帮助。谢谢。

英文:

I am using a datatable with dropdown filter for my project where I am popoluating the dropdown menu dynamically from the datatable. Automated 508 complaince tool is throwing the following error for the 3 dropdowns- "Elements with No Accessible Name: (3)". It also provides link to the following site on how to fix the error. https://www.ssa.gov/accessibility/andi/help/alerts.html?no_name_form_element.

I am not sure how to add labels since I am not defining the select options in the HTML. It is coming from Javascipt. Please see the code below.
Any guidance will be very helpful. Thanks.

https://live.datatables.net/finezici/1/edit

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
$(&#39;#table-wrap&#39;).hide();
  var table = $(&#39;#example&#39;).DataTable({

    responsive: true,
mark: true,
stateSave: false,
    searching: true
  });

  buildSelect(table);
  $(&#39;#table-filter&#39;).on(&#39;change&#39;, function () {
    // show the tbody when the user clicks on a filter option
$(&#39;#table-wrap&#39;).show();
    table.columns.adjust();

    table.search(this.value).draw();
  });

 
  table.on(&#39;draw&#39;, function() {
    buildSelect(table);
  });
  $(&#39;#test&#39;).on(&#39;click&#39;, function() {
    table.search(&#39;&#39;).columns().search(&#39;&#39;).draw();
    $(&#39;#table-wrap&#39;).hide();
  });
});

function buildSelect(table) {
  var counter = 0;
  table.columns([0, 1, 2]).every(function() {
    var column = table.column(this, {
      search: &#39;applied&#39;
    });
    counter++;
    var select = $(&#39;&lt;select&gt;&lt;option value=&quot;&quot;&gt;Select&lt;/option&gt;&lt;/select&gt;&#39;)
      .appendTo($(&#39;#dropdown&#39; + counter).empty())
      .on(&#39;change&#39;, function() {
        var val = $.fn.dataTable.util.escapeRegex(
          $(this).val()
        );

        column
          .search(val ? &#39;^&#39; + val + &#39;$&#39; : &#39;&#39;, true, false)
          .draw();
      });

    column.data().unique().sort().each(function(d, j) {
select.append(&#39;&lt;option value=&quot;&#39; + d + &#39;&quot;&gt;&#39; + d + &#39;&lt;/option&gt;&#39;);

    });

    // The rebuild will clear the exisiting select, so it needs to be repopulated
    var currSearch = column.search();
    if (currSearch) {
           // Use RegEx to find the selected value from the unique values of the column.
      // This will use the Regular Expression returned from column.search to find the first matching item in column.data().unique
      select.val(column.data().unique().toArray().find((e) =&gt; e.match(new RegExp(currSearch))));
    }
  });
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;script src=&quot;https://code.jquery.com/jquery-1.11.3.min.js&quot;&gt;&lt;/script&gt;

    &lt;link href=&quot;https://nightly.datatables.net/css/jquery.dataTables.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
    &lt;script src=&quot;https://nightly.datatables.net/js/jquery.dataTables.js&quot;&gt;&lt;/script&gt;

    &lt;meta charset=utf-8 /&gt;
    &lt;title&gt;DataTables - JS Bin&lt;/title&gt;
  &lt;/head&gt;
&lt;div class=&quot;searchbox&quot; select id=&quot;table-filter&quot;&gt;
&lt;p&gt;Name: &lt;span id=&quot;dropdown1&quot;&gt;
  &lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;Postion: &lt;span id=&quot;dropdown2&quot;&gt;
  &lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;Office: &lt;span id=&quot;dropdown3&quot;&gt;
&lt;/span&gt;
&lt;/p&gt;
  &lt;button type=&quot;button&quot;  id=&quot;test&quot;&gt;Clear Filters&lt;/button&gt;
&lt;/div&gt;
  &lt;div id=&quot;table-wrap&quot;&gt;
  &lt;table id=&quot;example&quot; class=&quot;cell-border row-border stripe dataTable no-footer dtr-inline&quot; role=&quot;grid&quot; style=&quot; width: 100%; padding-top: 10px;&quot;&gt;&lt;thead&gt;
&lt;tr&gt;

&lt;th&gt;&amp;#160;&lt;/th&gt;
&lt;th&gt;&amp;#160;&lt;/th&gt;
&lt;th&gt;&amp;#160;&lt;/th&gt;
&lt;th colspan=&quot;3&quot; style=&quot; text-align: center;&quot;&gt;Information&lt;/th&gt;
&lt;/tr&gt;


          &lt;tr&gt;
            &lt;th&gt;Name&lt;/th&gt;
            &lt;th&gt;Position&lt;/th&gt;
            &lt;th&gt;Office&lt;/th&gt;
            &lt;th&gt;Age&lt;/th&gt;
            &lt;th&gt;Start date&lt;/th&gt;
            &lt;th&gt;Salary&lt;/th&gt;
          &lt;/tr&gt;
        &lt;/thead&gt;

        
        &lt;tbody&gt;
          &lt;tr&gt;
            &lt;td&gt;ID.AI&lt;/td&gt;
            &lt;td&gt;System Architect&lt;/td&gt;
            &lt;td&gt;Edinburgh&lt;/td&gt;
            &lt;td&gt;61&lt;/td&gt;
            &lt;td&gt;2011/04/25&lt;/td&gt;
            &lt;td&gt;$3,120&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td&gt;Garrett -2&lt;/td&gt;
            &lt;td&gt;Director&lt;/td&gt;
            &lt;td&gt;Edinburgh&lt;/td&gt;
            &lt;td&gt;63&lt;/td&gt;
            &lt;td&gt;2011/07/25&lt;/td&gt;
            &lt;td&gt;$5,300&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td&gt;Ashton.1 -2&lt;/td&gt;
            &lt;td&gt;Technical Author&lt;/td&gt;
            &lt;td&gt;San Francisco&lt;/td&gt;
            &lt;td&gt;66&lt;/td&gt;
            &lt;td&gt;2009/01/12&lt;/td&gt;
            &lt;td&gt;$4,800&lt;/td&gt;
          &lt;/tr&gt;
          
            
          &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

它要求你为下拉菜单添加标签。要为label添加for属性,你需要为下拉菜单设置ID。

你可以在js中设置它,如下所示:

var select = $('<select id="dd' + counter + '"><option value="">Select</option></select>')

然后,你需要在label标签中使用动态生成的ID包装你的标签,看起来像这样:链接

英文:

It is asking you to give labels for your dropdowns. To add the label for attribute you need IDs for dropdowns.
You can set it in your js as with var select = $(&#39;&lt;select id=&quot;dd&#39; + counter + &#39;&quot;&gt;&lt;option value=&quot;&quot;&gt;Select&lt;/option&gt;&lt;/select&gt;&#39;)

Then you need to wrap your labels in a &lt;label&gt; tag using the dynamically generated ID in the for attribute.

Here is what it will look like: https://live.datatables.net/finezici/4/edit

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

$(document).ready(function() {
$(&#39;#table-wrap&#39;).hide();
  var table = $(&#39;#example&#39;).DataTable({

    responsive: true,
mark: true,
stateSave: false,
    searching: true
  });

  buildSelect(table);
  $(&#39;#table-filter&#39;).on(&#39;change&#39;, function () {
    // show the tbody when the user clicks on a filter option
$(&#39;#table-wrap&#39;).show();
    table.columns.adjust();

    table.search(this.value).draw();
  });

 
  table.on(&#39;draw&#39;, function() {
    buildSelect(table);
  });
  $(&#39;#test&#39;).on(&#39;click&#39;, function() {
    table.search(&#39;&#39;).columns().search(&#39;&#39;).draw();
    $(&#39;#table-wrap&#39;).hide();
  });
});

function buildSelect(table) {
  var counter = 0;
  table.columns([0, 1, 2]).every(function() {
    var column = table.column(this, {
      search: &#39;applied&#39;
    });
    counter++;
    var select = $(&#39;&lt;select id=&quot;dd&#39; + counter + &#39;&quot;&gt;&lt;option value=&quot;&quot;&gt;Select&lt;/option&gt;&lt;/select&gt;&#39;)
      .appendTo($(&#39;#dropdown&#39; + counter).empty())
      .on(&#39;change&#39;, function() {
        var val = $.fn.dataTable.util.escapeRegex(
          $(this).val()
        );

        column
          .search(val ? &#39;^&#39; + val + &#39;$&#39; : &#39;&#39;, true, false)
          .draw();
      });

    column.data().unique().sort().each(function(d, j) {
select.append(&#39;&lt;option value=&quot;&#39; + d + &#39;&quot;&gt;&#39; + d + &#39;&lt;/option&gt;&#39;);

    });

    // The rebuild will clear the exisiting select, so it needs to be repopulated
    var currSearch = column.search();
    if (currSearch) {
           // Use RegEx to find the selected value from the unique values of the column.
      // This will use the Regular Expression returned from column.search to find the first matching item in column.data().unique
      select.val(column.data().unique().toArray().find((e) =&gt; e.match(new RegExp(currSearch))));
    }
  });
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;script src=&quot;https://code.jquery.com/jquery-1.11.3.min.js&quot;&gt;&lt;/script&gt;

    &lt;link href=&quot;https://nightly.datatables.net/css/jquery.dataTables.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
    &lt;script src=&quot;https://nightly.datatables.net/js/jquery.dataTables.js&quot;&gt;&lt;/script&gt;

    &lt;meta charset=utf-8 /&gt;
    &lt;title&gt;DataTables - JS Bin&lt;/title&gt;
  &lt;/head&gt;
&lt;div class=&quot;searchbox&quot; select id=&quot;table-filter&quot;&gt;
&lt;p&gt;&lt;label for=&quot;dd1&quot;&gt;Name:&lt;/label&gt; &lt;span id=&quot;dropdown1&quot;&gt;
  &lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;dd2&quot;&gt;Postion:&lt;/label&gt; &lt;span id=&quot;dropdown2&quot;&gt;
  &lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;dd3&quot;&gt;Office:&lt;/label&gt; &lt;span id=&quot;dropdown3&quot;&gt;
&lt;/span&gt;
&lt;/p&gt;
  &lt;button type=&quot;button&quot;  id=&quot;test&quot;&gt;Clear Filters&lt;/button&gt;
&lt;/div&gt;
  &lt;div id=&quot;table-wrap&quot;&gt;
  &lt;table id=&quot;example&quot; class=&quot;cell-border row-border stripe dataTable no-footer dtr-inline&quot; role=&quot;grid&quot; style=&quot; width: 100%; padding-top: 10px;&quot;&gt;&lt;thead&gt;
&lt;tr&gt;

&lt;th&gt;&amp;#160;&lt;/th&gt;
&lt;th&gt;&amp;#160;&lt;/th&gt;
&lt;th&gt;&amp;#160;&lt;/th&gt;
&lt;th colspan=&quot;3&quot; style=&quot; text-align: center;&quot;&gt;Information&lt;/th&gt;
&lt;/tr&gt;


          &lt;tr&gt;
            &lt;th&gt;Name&lt;/th&gt;
            &lt;th&gt;Position&lt;/th&gt;
            &lt;th&gt;Office&lt;/th&gt;
            &lt;th&gt;Age&lt;/th&gt;
            &lt;th&gt;Start date&lt;/th&gt;
            &lt;th&gt;Salary&lt;/th&gt;
          &lt;/tr&gt;
        &lt;/thead&gt;

        
        &lt;tbody&gt;
          &lt;tr&gt;
            &lt;td&gt;ID.AI&lt;/td&gt;
            &lt;td&gt;System Architect&lt;/td&gt;
            &lt;td&gt;Edinburgh&lt;/td&gt;
            &lt;td&gt;61&lt;/td&gt;
            &lt;td&gt;2011/04/25&lt;/td&gt;
            &lt;td&gt;$3,120&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td&gt;Garrett -2&lt;/td&gt;
            &lt;td&gt;Director&lt;/td&gt;
            &lt;td&gt;Edinburgh&lt;/td&gt;
            &lt;td&gt;63&lt;/td&gt;
            &lt;td&gt;2011/07/25&lt;/td&gt;
            &lt;td&gt;$5,300&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td&gt;Ashton.1 -2&lt;/td&gt;
            &lt;td&gt;Technical Author&lt;/td&gt;
            &lt;td&gt;San Francisco&lt;/td&gt;
            &lt;td&gt;66&lt;/td&gt;
            &lt;td&gt;2009/01/12&lt;/td&gt;
            &lt;td&gt;$4,800&lt;/td&gt;
          &lt;/tr&gt;
          
            
          &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月15日 21:05:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定