admin管理员组文章数量:1386672
The following code makes me click twice to navigate to the page I need. But the problem is that I only want to select the option once, and as soon as I make my selection I want to navigate to the page I need. I see that no option is selected. I have to either press enter or click again on the option.
On change doesn't give me a keycode, that I need to use later on, if the user is blind and cannot use the mouse, he/she has to navigate only using the tab key. Onchange will trigger an immediate event, without letting the user chose and option. That is the reason why I cannot use OnChange since it's not consistent for visually impaired users on IE11
$(document).on("click", '.js-jump-to-page', function(e) {
var url = $(this).val();
var target = '_blank';
if (url) {
alert(url);
window.open(url, target);
}
});
<script src=".1.1/jquery.min.js"></script>
<select name="select-rates-jump-to-page-1" class="select js-jump-to-page">
<option tabindex="0" value="">Select a product</option>
<option tabindex="0" value="www.google">Bla Bla 1</option>
<option tabindex="0" value="www.yahoo">Bla Bla 2</option>
<option tabindex="0" value="www.bing">Bla Bla 3</option>
</select>
The following code makes me click twice to navigate to the page I need. But the problem is that I only want to select the option once, and as soon as I make my selection I want to navigate to the page I need. I see that no option is selected. I have to either press enter or click again on the option.
On change doesn't give me a keycode, that I need to use later on, if the user is blind and cannot use the mouse, he/she has to navigate only using the tab key. Onchange will trigger an immediate event, without letting the user chose and option. That is the reason why I cannot use OnChange since it's not consistent for visually impaired users on IE11
$(document).on("click", '.js-jump-to-page', function(e) {
var url = $(this).val();
var target = '_blank';
if (url) {
alert(url);
window.open(url, target);
}
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="select-rates-jump-to-page-1" class="select js-jump-to-page">
<option tabindex="0" value="">Select a product</option>
<option tabindex="0" value="www.google.">Bla Bla 1</option>
<option tabindex="0" value="www.yahoo.">Bla Bla 2</option>
<option tabindex="0" value="www.bing.">Bla Bla 3</option>
</select>
Share
Improve this question
edited Mar 5, 2018 at 19:56
Monica
asked Mar 5, 2018 at 19:28
MonicaMonica
1,5344 gold badges28 silver badges46 bronze badges
4
- 2 use change not click – Mohammed Elhag Commented Mar 5, 2018 at 19:34
-
Does using
keypress
work then? - I mean, the user makes a selection from theselect
menu and then how do we know the selection is the final? the user presses theenter
key and we navigate away to the selected page/url? – blurfus Commented Mar 5, 2018 at 20:06 - @ochi aria labels are used with a screenreader to let know the user it's options and selections. But I cannot make it to work for a regular user. If you see my example. Isn't it weird that you have to click again on the option that was once previously selected? – Monica Commented Mar 5, 2018 at 20:11
-
I think I understand that part, I just do not understand how to determine that an option selected in the
select
box is the final one. What would be the process like? – blurfus Commented Mar 5, 2018 at 20:17
3 Answers
Reset to default 4You need to use change
instead of the click
event.
$(document).on("change", '.js-jump-to-page', function(e) {
var url = $(this).val();
var target = '_blank';
if (url) {
alert(url);
window.open(url, target);
}
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="select-rates-jump-to-page-1" class="select js-jump-to-page">
<option tabindex="0" value="">Select a product</option>
<option tabindex="0" value="www.google.">Bla Bla 1</option>
<option tabindex="0" value="www.yahoo.">Bla Bla 2</option>
<option tabindex="0" value="www.bing.">Bla Bla 3</option>
</select>
I think the keypress
event might work here.
Once the user has selected an option, the user can press the enter key and we navigate away to the selected page/url
Kind of like this
$(function() {
$(document).on("keypress", '.js-jump-to-page', function(e) {
// check if the keypress is 'Enter'
if (e.which === 13) {
// get value of selected
var selectedUrl = $(".js-jump-to-page").val();
if (selectedUrl) {
// now go to that page
var target = '_blank';
//close dropdown (not sure this works like this)
$(".js-jump-to-page").blur();
console.log('Going to: ' + selectedUrl);
window.open(selectedUrl, target);
}
}
});
$(document).on("click", '.js-jump-to-page', function(e) {
var url = $(this).val();
e.preventDefault();
console.log('selected: ' + url);
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="select-rates-jump-to-page-1" class="select js-jump-to-page">
<option tabindex="0" value="">Select a product</option>
<option tabindex="0" value="www.google.">google</option>
<option tabindex="0" value="www.yahoo.">yahoo</option>
<option tabindex="0" value="www.bing.">bing</option>
</select>
Why not use select on change event
$( ".select" ).on('change', function() {
var url = $(this).val();
var target = '_blank';
if (url) {
alert(url);
window.open(url, target);
}
});
本文标签: javascriptWhy do I have to click twice on selected optionStack Overflow
版权声明:本文标题:javascript - Why do I have to click twice on selected option? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744563749a2612920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论