[英]Can I map over an array and turn variable into several different values?
I need to make variable i equal to 1,2,3,4. 我需要使变量i等于1,2,3,4。 As this is then used as part of the name of div that needs to be hidden/shown.
然后将其用作需要隐藏/显示的div名称的一部分。
Is there that this can be implemented easily? 是否可以轻松实现? I have been trying with loops, mapping over array etc. Any help would be greatly appreciated.
我一直在尝试循环,映射到数组等。任何帮助将不胜感激。
function listenForIconModalChange() {
// var modalNumbers = [1, 2, 3, 4];
// for (i = 0; i <= modalNumbers.length; i ++);
$('div[data-index^="icons_icon_"' + i + '"_modal_img"]').hide();
$('div[data-index^="icons_icon_"' + i + '"modal_img_adobe"]').hide();
$('div[data-index^="icons_icon_"' + i + '"_modal_imga"]').hide();
$('div[data-index^="icons_icon_"' + i + '"_modal_copy"]').hide();
$('div[data-index="icons_icon_"' + i + '"_modal"]').show();
enableIconModalDropdown = $('select[name="icons_icon_' + i + '"modal"]');
var enableIconModal = enableIconModalDropdown.val();
if (enableIconModal == 1) {
$('div[data-index^="icons_icon_"' + i + '"_modal_img"]').show();
$('div[data-index^="icons_icon_"' + i + '"modal_img_adobe"]').show();
$('div[data-index^="icons_icon_"' + i + '"_modal_imga"]').show();
$('div[data-index^="icons_icon_"' + i + '"_modal_copy"]').show();
}
enableIconModal1Dropdown.change(function() {
$('div[data-index^="icons_icon_"' + i + ']').hide();
$('div[data-index="icons_icon_"' + i + ']').show();
var enableIconModal = enableIconModal1Dropdown.val();
if (enableIconModal == 1) {
$('div[data-index="icons_icon_"' + i + ']').show();
}
});
}
You can use the ES6 template string, like this: 您可以使用ES6模板字符串,如下所示:
$(`div[data-index^="icons_icon_${i}_modal_img"]`);
Or if you want to keep your syntax, you have to remove some quotes: 或者,如果您想保留语法,则必须删除一些引号:
$('div[data-index^="icons_icon_' + i + '_modal_img"]');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.