输入文本时,在线键盘禁用

时间:2011-07-25 00:25:48

标签: jquery keyboard textarea

我正在使用本教程中略微增强的虚拟键盘:

http://net.tutsplus.com/tutorials/javascript-ajax/creating-a-keyboard-with-css-and-jquery/

Jquery:

$(function(){
var $write2 = $('#write2'),
    shift = false,
    capslock = false;

$('#keyboard li').click(function(){
    var $this = $(this),
        character = $this.html(); // If it's a lowercase letter, nothing happens to this variable

    // Shift keys
    if ($this.hasClass('left-shift') || $this.hasClass('right-shift')) {
        $('.letter').toggleClass('uppercase');
        $('.symbol span').toggle();

        shift = (shift === true) ? false : true;
        capslock = false;
        return false;
    }

    // Caps lock
    if ($this.hasClass('capslock')) {
        $('.letter').toggleClass('uppercase');
        capslock = true;
        return false;
    }

    // Delete
    if ($this.hasClass('delete')) {
        var html = $write2.html();

        $write2.html(html.substr(0, html.length - 1000));
        return false;
    }

    // Special characters
    if ($this.hasClass('symbol')) character = $('span:visible', $this).html();
    if ($this.hasClass('space')) character = $('span:visible', $this).html();
    if ($this.hasClass('tab')) character = $('span:visible', $this).html();
    if ($this.hasClass('return')) character = "\n";

    // Uppercase letter
    if ($this.hasClass('uppercase')) character = character.toUpperCase();

    // Remove shift once a key is clicked.
    if (shift === true) {
        $('.symbol span').toggle();
        if (capslock === false) $('.letter').toggleClass('uppercase');

        shift = false;
    }

    // Add the character
    $write2.html(character+ "  " + $write2.html());
    }); });

问题是,当我手动更改textarea时,键盘被禁用。我不确定为什么,教程没有真正解释这个bug。

感谢。

1 个答案:

答案 0 :(得分:1)

尝试使用val而不是html

$write.val($write.val() + character)