开发者

Better jQuery statement?

开发者 https://www.devze.com 2023-04-01 10:00 出处:网络
I\'m trying to get the (comma delimited) string of values inside of hdnCheckedPhoneNumbers,take each value, and do something with it.This works, but it feels a bit dirty.

I'm trying to get the (comma delimited) string of values inside of hdnCheckedPhoneNumbers, take each value, and do something with it. This works, but it feels a bit dirty.

    $($(".hdnCheckedPhoneNumbers").val().split(",")).each(function(index, val) {
        $(".phoneCB[data-rowNu开发者_开发问答mber='" + val + "']").children().attr("checked", "checked");
    });


Slightly optimized (but could be optimized further, specifically by using a for loop instead of jQuery.each()):

var numbers = $(".hdnCheckedPhoneNumbers").val().split(","),
    $phoneCb = $(".phoneCB");

$(numbers).each(function(index, val) {
    $phoneCb.filter("[data-rowNumber='" + val + "']").children().attr("checked", "checked");
});


Maybe this way....

var checkedNumbers  = $(".hdnCheckedPhoneNumbers").val().split(",");

var processCheckedNumbers = function(index, val) {
        $(".phoneCB[data-rowNumber='" + val + "']").children().attr("checked", "checked");
 }

$(checkedNumbers).each(processCheckedNumbers);
0

精彩评论

暂无评论...
验证码 换一张
取 消