简体   繁体   English

检查“数据”属性是否包含输入值的一部分(jQuery)

[英]Check if "data" attribute contains A PART of input value (jQuery)

With this way I only find the needed div, if the "data-content" contains the complete word.通过这种方式,如果"data-content"包含完整的单词,我只会找到所需的 div。

var input = "TestText123";
var found = $("#list").find(".box[data-name='" + input + "']").addClass("found");

But I also want to find this div if my "input" just contains a correct part of it (min. 5 characters).但如果我的“输入”只包含它的正确部分(最少 5 个字符),我也想找到这个 div。

It's interesting, that this would work, if I don't use the "data" method (but I need it).有趣的是,如果我不使用“数据”方法(但我需要它),这会起作用。

var found = $("#list").find(".box:contains('" + input + "')").addClass("found");

How to modify the "data" method in jQuery, like the way I need it?如何修改 jQuery 中的“数据”方法,就像我需要的那样?

Add an asterisk before the equal sign: [data-name*=...] .在等号前添加一个星号: [data-name*=...]

Side note: the argument of addClass should not have a dot, so just addClass("found") :旁注: addClass的参数不应该有一个点,所以只需addClass("found")

 var input = "TestText123"; var found = $("#list").find(".box[data-name*='" + input + "']").addClass("found");
 .found { background: yellow }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul id="list"> <li class ="box" data-name="WhatTestText12345">should match</li> </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM