【发布时间】:2018-11-07 20:06:29
【问题描述】:
有点初学者的问题。我正在尝试为 Drupal 内容发布页面应用 javascript,如果用户从多项选择下拉菜单中选择 X,则显示 div 1,2 和 4。如果他们选择 Y 和 Z,则显示这些相关字段.
我已经有了一个基于单一选择的基本工作脚本(即使有点丑陋和初学者)。但我有一个多项选择下拉菜单。
我想这是通过将选定的选项放入一个数组并对其进行检查来完成的。但这有点超出我的技能,我似乎无法为我的场景找到一个好的例子:
哎呀,我什至无法通过显示一系列选项的警报,更不用说根据需要更改代码了。我看到使用“$(this).val();”的建议...但我只是在控制台中收到一个错误:“$ 不是函数。”假设我做对了,我已经尝试过 jQuery 语法和 javascript 语法。我很确定我的 Drupal 模块知道接受 jQuery,如果这是问题的话。
(作为旁注,我确信还有一种更简单/更短的方法可以使用类或自定义属性来切换 div。但是,对于所有不同的 ID 和场景,这些方法似乎并没有更短.)
作为 Javascript 的新手,我希望我已经解释得很好。这是我制作并想要更改的脚本:
if(window.attachEvent) {
window.attachEvent('onload', whenLoaded);
} else {
if(window.onload) {
var curronload = window.onload;
var newonload = function(evt) {
curronload(evt);
whenLoaded(evt);
};
window.onload = newonload;
} else {
window.onload = whenLoaded;
}
}
function whenLoaded() {
document.getElementById('edit-field-resource-type').addEventListener('change', function () {
alert(this.value); // test to show worked.
// alert($(this).val()); // tried for multiple choices - didn't work:
// "$ is not a function" .. I tried this way too: $(this).value;
// var choices_array = [];
var showWebinars = this.value == "taxonomy_term-14" ? 'block' : 'none';
var showCaseStudies = this.value == "taxonomy_term-12" ? 'block' : 'none';
var showWhitePapers = this.value == "taxonomy_term-11" ? 'block' : 'none';
var showVideos = this.value == "taxonomy_term-13" ? 'block' : 'none';
var showPodcasts = this.value == "taxonomy_term-62" ? 'block' : 'none';
var showOther = this.value == "taxonomy_term-16" ? 'block' : 'none';
var showInfographics = this.value == "taxonomy_term-10" ? 'block' : 'none';
document.getElementById('edit-post-webinar-copy-wrapper').style.display = showWebinars;
document.getElementById('edit-field-event-duration-wrapper').style.display = showWebinars;
document.getElementById('edit-field-infographic-file-wrapper').style.display = showInfographics;
document.getElementById('edit-field-video-embed-wrapper').style.display = showVideos;
document.getElementById('edit-field-video-third-party-wrapper').style.display =
document.getElementById('edit-field-audio-file-wrapper').style.display = showPodcasts;
document.getElementById('edit-field-audio-file-link-wrapper').style.display = showPodcasts;
document.getElementById('edit-field-resource-file-wrapper').style.display = showWhitePapers;
document.getElementById('edit-field-resource-file-wrapper').style.display = showWhitePapers;
document.getElementById('edit-field-infographic-file-wrapper').style.display = showInfographics;
});
}
【问题讨论】:
-
如果你想使用 jQuery 语法,你需要使用类似
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>的东西来要求 jQuery -
是的,我认为 Drupal 有一个不相关的问题,不包括以编程方式。现在,我将坚持使用 javascript 本身
-
我想我已经取得了一些进展,使用 Javascript(不是 jQuery)循环遍历选择并将结果(如果选择)添加到数组“selectedArray”中...现在,我要确定的是如何修改它下面的三元组 var x = this.value == term-in-array selectedArray ? 'block' : 'none' ...除非有更好的方法来解决这个问题?
-
对于 DOM 操作(这是你想要做的),我真的推荐使用 jQuery,因为它相对轻量级,你会避免很多小错误和跨浏览器的问题兼容性...我有一个想法,我会尽快发布。