[英]Reset Custom JavaScript drop-down list Without Page Refersh
I am using this code in order to customize a drop-down list Selector.我正在使用此代码来自定义下拉列表选择器。 Is there a way I can reset this code without having to reload the page by calling a function by pressing a button.
有没有一种方法可以重置此代码,而无需通过按下按钮调用函数来重新加载页面。
<button onclick="reset();">Reset</button>
Ex.前任。 If "Jaguar" is selected, when the button is pressed, the selected element should change back to the orginal "Select car:" 0 option.
如果选择了“Jaguar”,则按下按钮时,所选元素应变回原来的“Select car:” 0 选项。
reset = function() {
// Set default option to "Select car:" (0)
}
When reseting, what you want is to put the first option
text into the select-selected
div (which is the one that shows the current selected option) and also update the select
selected index.重置时,您想要的是将第一个
option
文本放入select-selected
div(显示当前所选选项的div)并更新select
selected index。
Also, you want to remove same-as-selected
class from the selected div:此外,您想从选定的 div 中删除
same-as-selected
类same-as-selected
类:
function reset() {
var customSelect = document.getElementsByClassName("custom-select")[0];
var select = customSelect.getElementsByTagName("select")[0];
var selected = customSelect.getElementsByClassName("select-selected")[0];
var selectedItem = customSelect.getElementsByClassName('same-as-selected')[0];
select.selectedIndex = 0;
selected.innerHTML = select.options[select.selectedIndex].innerHTML;
selectedItem.classList.remove('same-as-selected');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.