使用event.which来验证用户是否按下空格键在Firefox中不起作用 [英] Using event.which to verify if the user pressed the spacebar doesn't work in Firefox
问题描述
我尝试了很多在这里和这里找到的解决方案:
event = event || window.event // IE不会将事件传递给函数
if(event == window.event){
code = event.keyCode;
} else {
code = event.which;
if(code =='32'){}
但是它仍然没有在Firefox中工作。
我想我在Firefox中错误地调用了函数。看看整个脚本:
< textarea onkeydown =predicao(this); cols =40rows =10id =testonfocus =this.focus()>< / textarea>
< input id =examplestyle =display:none; onkeydown =javascript:insert(this);/>
< script language =Javascript>
<! -
function predicao(objeto){
comprimento = objeto.value.length;
var antipenultimo = comprimento - 4;
var input = objeto.value.substring(antipenultimo,comprimento);
var output =;
for(i = 0; i< input.length; ++ i){
if(output!=)output + =,;
output + = input.charCodeAt(i);
if(output ==91,91,103,32){
var preditor = document.getElementById('example');
preditor.value ='';
preditor.style.display ='block';
preditor.focus();
preditor.select();
函数insert(objeto){
event = event.which || window.event // IE不会将事件传递给函数
if(event == window.event){
code = event.keyCode;
} else {
code = event.charCode;
if(keynum =='32'){
var texto = document.getElementById('test')。value;
texto + = objeto.value +']]';
$('#test')。focus();
document.getElementById('test')。value = texto;
objeto.style.display ='none';
$ b $(document).ready(function(){
var data =Ab Aco Ado Ala Mano Cata Ca Obo Olo Po Poq.split( );
$(#example)。autocomplete(data);});
< / script>
我想要做的是 - (我不知道名字) - 预测帮助textarea内的输入器。它使用jQuery自动完成。
当用户输入'['g'在textarea(id = test)里面时,打开一个自动完成的输入(id = example),所以它在'data'中搜索。当用户找到所需的数据时,他必须按空格键将数据插入到textarea中,用']]结束,但在Firefox中不起作用。
(是的,我用完全错误的方式使用JavaScript和jQuery来处理相同的元素,因为我不太擅长这一点,所以我会在Firefox工作后尝试修正它。)
<解决方案编辑
HMTL:
< textarea onkeydown =predicao(this); cols =40rows =10id =testonfocus =this.focus()>< / textarea>
< input id =examplestyle =display:none; onkeydown =insert(this,event);/>
JS:
function predicao(objeto){
var comprimento = objeto.value.length;
var antipenultimo = comprimento - 4;
var input = objeto.value.substring(antipenultimo,comprimento);
var output =;
for(var i = 0; i< input.length; ++ i){
if(output!=)output + =,;
output + = input.charCodeAt(i);
if(output ==91,91,103,32){
var preditor = document.getElementById('example');
preditor.value ='';
preditor.style.display ='block';
preditor.focus();
preditor.select();
函数insert(objeto,evt){
var e = evt ||事件;
var code = e.keyCode || e.which;
if(code =='32'){
var texto = document.getElementById('test')。value;
texto + = objeto.value +']]';
$('#test')。focus();
document.getElementById('test')。value = texto;
objeto.style.display ='none';
$ b $(document).ready(function(){
var data =Ab Aco Ado Ala Mano Cata Ca Obo Olo Po Poq.split( );
$(#example)。autocomplete(data);});
我在这里使用了Alexander Kahoun和pimvdb发布的内容。
I want a script to verify if the key pressed is 'spacebar', keycode 32. I noticed that IE uses other function names.
I tried a lot of solutions found here and this:
event = event || window.event // IE does not pass event to the function
if(event == window.event){
code = event.keyCode;
}else{
code = event.which;
}
if(code == '32') {}
But it still didn't work in Firefox.
I think I'm calling the function wrongly in Firefox. Look at the entire script:
<textarea onkeydown="predicao(this);" cols="40" rows="10" id="test" onfocus="this.focus()"></textarea>
<input id="example" style="display: none;" onkeydown="javascript: insert(this);"/>
<script language="Javascript">
<!--
function predicao(objeto){
comprimento = objeto.value.length;
var antipenultimo = comprimento - 4;
var input = objeto.value.substring(antipenultimo,comprimento);
var output = "";
for(i=0; i<input.length; ++i){
if(output != "") output += ", ";
output += input.charCodeAt(i);
}
if (output == "91, 91, 103, 32"){
var preditor = document.getElementById('example');
preditor.value = '';
preditor.style.display = 'block';
preditor.focus();
preditor.select();
}
}
function insert(objeto){
event = event.which || window.event // IE does not pass event to the function
if(event == window.event){
code = event.keyCode;
}else{
code = event.charCode;
}
if(keynum == '32') {
var texto = document.getElementById('test').value;
texto += objeto.value+']]';
$('#test').focus();
document.getElementById('test').value = texto;
objeto.style.display = 'none';
}
}
$(document).ready(function(){
var data = "Ab Aco Ado Ala Mano Cata Ca Obo Olo Po Poq".split(" ");
$("#example").autocomplete(data);});
</script>
What I'm trying to do is - (I don't know the name) - a Prediction Help Inputter inside a textarea. It uses jQuery autocomplete. When the user types '[[g ' inside textarea (id=test), a input with autocomplete is opened (id=example), so it search in 'data'. When the user find the desired data, he must press spacebar to insert the data into the textarea, closing with ']]' But it doesn't work in Firefox.
(And yes, I'm using JavaScript and jQuery to same elements in a totally wrong way because I'm not too good at this, I'll try to correct it after Firefox works.)
EDIT
HMTL:
<textarea onkeydown="predicao(this);" cols="40" rows="10" id="test" onfocus="this.focus()"></textarea>
<input id="example" style="display: none;" onkeydown="insert(this, event);"/>
JS:
function predicao(objeto){
var comprimento = objeto.value.length;
var antipenultimo = comprimento - 4;
var input = objeto.value.substring(antipenultimo,comprimento);
var output = "";
for(var i=0; i<input.length; ++i){
if(output != "") output += ", ";
output += input.charCodeAt(i);
}
if (output == "91, 91, 103, 32"){
var preditor = document.getElementById('example');
preditor.value = '';
preditor.style.display = 'block';
preditor.focus();
preditor.select();
}
}
function insert(objeto, evt){
var e = evt || event;
var code = e.keyCode || e.which;
if(code == '32') {
var texto = document.getElementById('test').value;
texto += objeto.value+']]';
$('#test').focus();
document.getElementById('test').value = texto;
objeto.style.display = 'none';
}
}
$(document).ready(function(){
var data = "Ab Aco Ado Ala Mano Cata Ca Obo Olo Po Poq".split(" ");
$("#example").autocomplete(data);});
I used here what Alexander Kahoun and pimvdb have posted.
这篇关于使用event.which来验证用户是否按下空格键在Firefox中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!