开发者

Better jQuery statement?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜