【发布时间】:2019-12-15 11:18:12
【问题描述】:
到目前为止,我只能找到搜索包含特定数据项值的元素的解决方案。相反,我需要找到任何元素(当前为 input 或 select),其中包含一个名为 data-showsubmenutypes 的数据项,该数据项也具有任何值。
到目前为止,我的解决方案是使用两个单独的搜索(确实有效):
$(".ContactForm fieldset input[data-showsubmenutypes], .ContactForm fieldset select[data-showsubmenutypes]").each(function () {
// myArray.push($(this).data("showsubmenutypes"));
});
但是,它冗长且难以阅读。 有什么办法可以简化以上内容?
我尝试了几十次,都返回undefined错误,例如:
$(".ContactForm fieldset").data("showsubmenutypes").each(function () {
$(".ContactForm fieldset *").data("showsubmenutypes").each(function () {
$(".ContactForm fieldset [data-showsubmenutypes]").each(function () {
【问题讨论】:
-
只是使用属性选择器
".ContactForm fieldset [data-showsubmenutypes]"不知道为什么你最后一次尝试没有成功。