【问题标题】:How to use String.prototype.startsWith filter with html table如何将 String.prototype.startsWith 过滤器与 html 表一起使用
【发布时间】:2018-12-29 23:16:37
【问题描述】:

如何在 HTML 表格中使用 String.prototype.startsWith 过滤器

这是我的项目使用 json 的示例,但我必须在我的项目中使用过滤器数组。

http://jsfiddle.net/8kkg3/3471/

这是我想在表格中使用的过滤器代码。

if (typeof String.prototype.startsWith != 'function') {
            String.prototype.startsWith = function (str) {
                return this.substring(0, str.length) === str;
            }
        };
        const str ='John';
        let result =str.startsWith('j');
        console.log(result);

【问题讨论】:

  • J 不是j。尝试先拨打toLowerCase

标签: javascript arrays html filter


【解决方案1】:
"J" == "j"

上述陈述将被评估为 False。下面的resultAresultB 都是True。

const str = 'John';
let resultA = str.startsWith('J');
let resultB = str.toLowerCase().startsWith('j');

console.log(resultA);
console.log(resultB);

【讨论】:

  • 谢谢你的这一点,你知道如何将它与这个代码与html表格一起使用并通过输入文本框jsfiddle.net/8kkg3/3471进行过滤
  • 这是来自 w3school 的example
【解决方案2】:

Here's 更新您的小提琴,它根据输入过滤表格。


顺便说一句,您的标记有点偏离,您不应该将标题“用户名”放入表格正文中。您还缺少 tbodythead 元素。

此外,var 的使用被视为已弃用。您应该使用 letconst 代替(see here 作为指南)。

【讨论】:

  • 非常感谢 :) 我将使用 const
  • 另一个问题,如何在表格中添加另一个输入过滤器,已经输入了用户名。现在,我需要为 Job 添加另一个输入,并更新我的文件。 jsfiddle.net/aliaati/8kkg3/3536
  • 您只需要复制、粘贴和调整: 1. 为作业输入添加输入处理程序$(...).on('change', f); 2. 在处理程序 `f` 中应用与名称更改相同的逻辑,但为作业列使用适当的选择器。
猜你喜欢
  • 2021-04-02
  • 1970-01-01
  • 2019-09-26
  • 2012-01-28
  • 2017-02-27
  • 2023-03-13
  • 2011-11-04
  • 1970-01-01
  • 2017-08-17
相关资源
最近更新 更多