preventDefault() not working on SELECT elements in Firefox 9.0.1

怎甘沉沦 提交于 2019-12-10 12:37:06

问题


I realise there are other questions on preventDefault() not working with Firefox, but they did not help me.

I've got three SELECT lists, and all I want is to navigate between them using the arrow keys without changing any values. The code works great in Chrome, but in Firefox it moves focus and then changes the value on the element just moved to.

http://jsbin.com/ofitif/3/edit

JavaScript:

$(document).ready(function () { 
  $('.myinput').keydown(function (evt) { onkeydown(evt); });
  $('.myinput:first').focus(); 
}); 

function onkeydown(evt) {
  evt.preventDefault();
  console.log(evt.which);
  if(evt.which == 39) {
    $(document.activeElement).next().focus();
  }
  else if(evt.which == 37) {
    $(document.activeElement).prev().focus();
  }             
}

HTML:

<div id="inputs">
    <select class="myinput">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </select>
    <select class="myinput">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </select>
    <select class="myinput">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </select>
</div>

回答1:


nsListControlFrame::KeyPress in http://mxr.mozilla.org/mozilla-central/source/layout/forms/nsListControlFrame.cpp doesn't seem to check whether the default action was prevented. File a bug?




回答2:


according to your requirement try passing value for prevent default..it will be something like below..

evt.preventDefault ? evt.preventDefault() : evt.returnValue = false;

it worked for me plz try it.......




回答3:


Return false from your keydown handler:

$('.myinput').keydown(function (evt) { onkeydown(evt); return false; });


来源:https://stackoverflow.com/questions/8870037/preventdefault-not-working-on-select-elements-in-firefox-9-0-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!