我正在尝试在hdnCheckedPhoneNumbers
中获取(逗号分隔的)值字符串,获取每个值,并对其执行某些操作。这有效,但感觉有点脏。
$($(".hdnCheckedPhoneNumbers").val().split(",")).each(function(index, val) {
$(".phoneCB[data-rowNumber='" + val + "']").children().attr("checked", "checked");
});
答案 0 :(得分:1)
也许这样......
var checkedNumbers = $(".hdnCheckedPhoneNumbers").val().split(",");
var processCheckedNumbers = function(index, val) {
$(".phoneCB[data-rowNumber='" + val + "']").children().attr("checked", "checked");
}
$(checkedNumbers).each(processCheckedNumbers);
答案 1 :(得分:1)
稍微优化(但可以进一步优化,特别是使用for循环而不是jQuery.each()
):
var numbers = $(".hdnCheckedPhoneNumbers").val().split(","),
$phoneCb = $(".phoneCB");
$(numbers).each(function(index, val) {
$phoneCb.filter("[data-rowNumber='" + val + "']").children().attr("checked", "checked");
});