gpt4 book ai didi

javascript - jQuery .data() 选择器

转载 作者:行者123 更新时间:2023-12-01 02:35:08 25 4
gpt4 key购买 nike

此问题之前已被问过,但我找不到自 2010 年 5 月以来给出的任何答案。如果其中一个或多个答案仍然相关,我很乐意删除此问题。

jQuery 是否有一个选择器,允许我按 .data() 添加的值进行过滤?

示例:(见评论)

//Set IsValid data attribute on all elements that have fired the blur event
$(".txtPayment").blur(function (e) {
if (IsCurrency($(this).val()))
$(this).data("IsValid", true);
else
$(this).data("IsValid", false);

SumPayment();
});

//Sum all payments

// I'd like to be able to select all inputs with:
// class == "txtPayment" value != "" && IsValid == true
$('.txtPayment[value!=""]').each(function ()
{
var sum = 0;

if ($(this).data("IsValid"))
{
sum += Number($(this).val());
$(this).removeClass("textError");
}
else
{
$(this).addClass("textError");
$("#spanSumPayment").addClass("textError");
}
});

这可以不使用插件吗?

最佳答案

Does jQuery have a selector that will allow me to filter by values added by .data()?

差不多了。它有一个函数可以做到这一点。

$(".txtPayment").blur(function () {
$(this).data("IsValid", IsCurrency($(this).val()) );
SumPayment();
});

var validOnes = $('.txtPayment[value!=""]').filter(function() {
return !!$(this).data("IsValid")); // (!! turns anything into a Boolean)
});

关于javascript - jQuery .data() 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7676262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com