jQuery 中如何检查一个元素是否包含另一个元素

发布于 2024-08-02 14:25:53 字数 370 浏览 3 评论 0原文

我将 xVal 与 jquery.validate 插件结合使用,但遇到了一个小问题,因为它在某个实例中发布了两次验证消息。我认为这个错误应该可以通过一些聪明的 jQuery 在错误消息的放置中相当容易地修复。

我正在尝试找到一种方法来查看 ul 是否已包含带有某些文本的 li 。

errorPlacement: function(error, element) {
     $("ul.errors:not(:contains(" + error + "))").append(error);
}

我认为像上面这样的东西可能会起作用,但没有那么幸运。 error.toString() 也不起作用。

任何帮助将不胜感激。

I am using xVal combind with the jquery.validate plugin, but have come across a slight problem in that it is posting validation messages twice in a certain instance. I think this bug should be fairly easily fixed with some clever jQuery in the placement of the error message.

I am trying to find a way to see if a ul already contains an li with some text in it.

errorPlacement: function(error, element) {
     $("ul.errors:not(:contains(" + error + "))").append(error);
}

I thought something like the above may work, but not such luck. error.toString() does not work either.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鯉魚旗 2024-08-09 14:25:53

看一下 :has 选择器:

$("ul:has(li:contains('" + error + "'))")

上面的选择器将选择 UL< /code> 元素,其中有一个包含 error 文本的 LI 元素。

Give a look to the :has selector:

$("ul:has(li:contains('" + error + "'))")

The above selector will select the UL elements which have a LI element that contains the error text.

活雷疯 2024-08-09 14:25:53

此处没有该插件,我无法判断获取错误文本的适当属性是什么,但您可以使用 Firebug 执行 errorconsole.log 并查看它是什么。

您还需要在字符串两边加上引号。

$("ul.errors:not(:contains('" + errorString + "'))")

...并且不要忘记转义错误消息中的任何单引号!

Not having that plugin here, I can't tell what the appropriate property is to get the error text, but you can use Firebug to do a console.log of error and see what it is.

You'll also need to put quotes around the string.

$("ul.errors:not(:contains('" + errorString + "'))")

...and don't forget to escape for any single quotes in the error message!

-黛色若梦 2024-08-09 14:25:53

您的代码中的错误是什么?
首先,您将其称为字符串 :contains(" + error + "),然后将其称为元素 - .append(error);.< br>
如果这是一个元素,请尝试使用 $(error).text() 获取其文本,并在 ul 中搜索文本。

What is error in your code?
First you refer to it as a string :contains(" + error + "), but then you refer to it as an element - .append(error);.
If this is an element, try getting its text by using $(error).text(), and search for the text in the ul.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文