Javascript 如何使用jquery操作select标记中的选项标记
我需要一些帮助。我有一个包含“汽车”和“商业”的下拉列表。 我需要做的是,如果我点击汽车,第二个下拉选项将被过滤。将显示最后一部分中带有字母“A”的所有选项 我只需要使用javascript或jquery 因此,在JQuery中(此块应存在于页面的脚本标记或导入的脚本文件中):Javascript 如何使用jquery操作select标记中的选项标记,javascript,jquery,Javascript,Jquery,我需要一些帮助。我有一个包含“汽车”和“商业”的下拉列表。 我需要做的是,如果我点击汽车,第二个下拉选项将被过滤。将显示最后一部分中带有字母“A”的所有选项 我只需要使用javascript或jquery 因此,在JQuery中(此块应存在于页面的脚本标记或导入的脚本文件中): 您应该在第一个下拉列表的onchange事件中附加一个事件侦听器,然后检查下拉列表中选择的选项文本,看看它是自动的还是商业的,然后在附加到onchange事件的函数中的下拉列表2中执行过滤请,我是jquery和javas
您应该在第一个下拉列表的onchange事件中附加一个事件侦听器,然后检查下拉列表中选择的选项文本,看看它是自动的还是商业的,然后在附加到onchange事件的函数中的下拉列表2中执行过滤请,我是jquery和javascript的新手。你能举个例子吗?在下面贴出一个答案,这会给你一个良好的开端,并希望能解释一些基础知识。我把下拉列表2的过滤留给了你,因为我从你的帖子中不明白你在过滤方面到底想要什么。那么过滤呢?如果单击汽车,将显示第二个下拉列表中的选项,最后显示字母a。我该怎么做?@Centi请添加到您的问题中,并给我一个在筛选之前和之后的第二个下拉列表项目的示例(您可以将其作为一个小示例)@Centi我更新了我的答案,以向您展示如何隐藏元素,现在您只需要学习如何再次显示它们并隐藏其他元素。我在我的问题中添加了一张照片。这就是一个例子。如果我点击汽车按钮。将显示第二个下拉列表中以A结尾的所有选项,并隐藏非A选项。但如果你点击广告,情况会相反。再次需要帮助。代码是以xsl文件格式编写的。我没有说。因此,这些选项形成了一个数据表。
//document.ready() fires once the DOM has fully loaded, in otherwords all elements of the document have been rendered,
//you can then access them to do what we are doing here, which is attaching an event listener to your 1st drop down,
//and then inside that event's function do your second drop downs filtering based on the selection in drop down 1
document.ready(function(){
//The pound sign indicates the Id property of the element
//In $(#dropDown1) replace the word dropDown1 with your drop down's actual Id
$('#dropDown1').on('change', function(){
//$(this) gets you the drop down element and .find() searches for the nearest decendant that meets the selector requirement,
//in this case element with tag option and attribute 'selected'
if($(this).find('option:selected').text() === 'Automotive'){
//Do filtering of drop down 2 for Automotive selection
//Iterate your options, if they dont have a last letter
//equal to a or A hide() the element
//Do similar action when Commercial selected
$('#dropDown2 option').each(function(i, ele){
let lastChar = $(ele).text()[$(ele).text().length - 1];
if(lastChar !== 'A' && lastChar !== 'a'){
$(ele).hide();
}
});
}else{
//Do filtering of drop down 2 for Commercial selection
//Change this to else if if you ever have more than 2 options
}
});
});