简体   繁体   English

如何检查我是否连续两次选择了相同的下拉列表选项?

[英]How do I check if I have selected the same drop-down list option twice in a row?

Let's supose I have a simple drop-down list from which I can freely select any option I want.假设我有一个简单的下拉列表,我可以从中自由选择我想要的任何选项。

I can, via JavaScript, store the selected option's value in a variable this way:我可以通过 JavaScript 以这种方式将所选选项的值存储在变量中:

 var checkValue = function(){ const mySelect = document.getElementById("cars"); let currentValue = mySelect.options[ mySelect.selectedIndex ].value; console.log(currentValue); }
 <select id="cars" onchange="checkValue()"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select>

Questions:问题:

  1. How can I check if I have selected the same drop-down list option twice in a row, in other words, if the value of currentValue has changed ?如何检查我是否连续两次选择了相同的下拉列表选项,换句话说, currentValue的值是否已更改

  2. Would using AngularJS make this task easier?使用 AngularJS 会让这个任务更容易吗? How?如何?

You might be looking for onclick event and then check whether the value is the same:您可能正在寻找 onclick 事件,然后检查值是否相同:

 var checkValue = function(){ let mySelect = document.getElementById("cars").options; let currentValue = mySelect[ mySelect.selectedIndex ].value; if (currentValue === previousValue) { console.log("The same option was selected"); } else { console.log("Different option"); } previousValue = currentValue; } let select = document.getElementById("cars").options; let previousValue = select[select.selectedIndex].value;
 <select id="cars"> <option value="volvo" onclick="checkValue(this)">Volvo</option> <option value="saab" onclick="checkValue(this)">Saab</option> <option value="opel" onclick="checkValue(this)">Opel</option> <option value="audi" onclick="checkValue(this)">Audi</option> </select>

 var arr = []; var checkValue = function() { const mySelect = document.getElementById("cars"); let currentValue = mySelect.options[mySelect.selectedIndex].value; if ($.inArray(currentValue, arr) != "-1") { console.log("already selected"); } else { arr.push(currentValue); console.log(currentValue); } }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <select id="cars" onchange="checkValue()"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select>

An example of triggering event on element select twice in a row might be to add click event instead of change event, I was thinking about MutationObserver but it seems this one doesn't trigger when listening to options change if its already selected check here连续两次在元素选择上触发事件的一个例子可能是添加点击事件而不是更改事件,我在考虑MutationObserver但如果它已经选择检查这里,似乎在侦听选项更改时不会触发

 var activities = document.getElementById("activitySelector"); activities.addEventListener("click", function() { console.log('selected item'); });
 <select id="activitySelector"> <option value="addNew">Item</option> <option value="1">option 1</option> </select>

with click event you can do this:通过单击事件,您可以执行以下操作:

 let selectedoptions = []; var clickListener = function(){ const mySelect = document.getElementById("cars"); let currentValue = mySelect.options[ mySelect.selectedIndex ].value; if(selectedoptions.indexOf(currentValue) < 0) { selectedoptions.push(currentValue); } else { console.log('selected before'); } //console.log(currentValue); }
 <select id="cars" onclick="clickListener()"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 jQuery、JavaScript 和 HTML 动态设置下拉列表的选定选项? - How do I dynamically set the selected option of a drop-down list using jQuery, JavaScript and HTML? 如何从下拉列表中隐藏选定的选项 - How can I Hide Selected option from Drop-down list 如何基于从下拉菜单中选择的选项以HTML显示文本? - How do I display Text in HTML based on an option selected from a Drop-Down Menu? (Django)如何在更改外键下拉列表后使“选定”选项有效? - (Django) How do I make the 'selected' option valid after altering a foreign key drop-down? 如何使用取决于另一个下拉列表中所选选项的下拉列表显示表格简码 - how to display table shortcode with a drop-down list that depends on the selected option in another drop-down list 如何检查是否在JavaScript中选择了下拉菜单 - How to check if drop-down is selected in JavaScript 在下拉列表中选择一个选项时,在表的行上显示输入文本框 - Show input text-box on the row of the table when an option is selected on a drop-down list 根据另一个下拉列表中的所选选项更改下拉列表中的项目 - Change items in a drop-down list depending on the selected option in another drop-down list 从SuiteScript的下拉菜单中选择选项后,如何显示文本框? - How do I make a text box appear after selecting an option from a drop-down menu in SuiteScript? 当用户未在下拉菜单上选择选项时,如何提醒用户 - how do I alert user when they didn't select an option on a drop-down menu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM