[英]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:问题:
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
的值是否已更改?
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.