javascript – 使用event.which验证用户是否按下空格键在Firefox中不起作用

我想要一个脚本来验证按下的键是否是’空格键’,键码32.我注意到IE使用其他函数名称.

我在这里尝试了很多解决方案,这个:

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') {}

但它仍然无法在Firefox中运行.

我想我在Firefox中错误地调用了这个函数.看一下整个脚本:

<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>

我想要做的是 – (我不知道名字) – textarea里面的预测帮助输入器.它使用jQuery自动完成.
当用户键入'[[g’在textarea(id = test)中时,会打开一个带有自动完成功能的输入(id = example),因此它会搜索’data’.当用户找到所需的数据时,他必须按空格键将数据插入textarea,以’]结束”但它在Firefox中不起作用.

(是的,我使用JavaScript和jQuery以完全错误的方式使用相同的元素,因为我不太擅长这个,我会尝试在Firefox工作之后纠正它.)

解决方法:

编辑

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);});

我在这里用过Alexander Kahoun和pimvdb发布的内容.

上一篇:求最值---数学+问题转化


下一篇:树状数组二分