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);
精彩评论