使用自定义键盘时Jquery重复功能

时间:2016-06-30 09:14:54

标签: javascript jquery html css

我希望我在这里错过了一些非常简单的东西,我正在使用以下教程中的代码:

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

该项目是为触摸屏设置弹出式键盘,我已经因各种原因进行了一些修改,每次单击单独的文本字段和输入键时,我都会在字段中获得双倍的字符。

以下代码:

<!DOCTYPE html>

<html>
  <head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="~/Content/keyboard_css.css" rel="stylesheet" />
  </head>
  <body>

    <div id="container">
        <label>Client Name</label><div class="tbx" id="tbx_1" contenteditable></div>
        <div class="tbx" id="tbx_2" contenteditable></div>
        <div class="tbx" id="tbx_3" contenteditable></div>
        <div class="tbx" id="tbx_4" contenteditable></div>
        <div class="tbx" id="tbx_5" contenteditable></div>

        <ul id="keyboard">
            <li class="symbol"><span class="off">`</span><span class="on">~</span></li>
            <li class="symbol"><span class="off">1</span><span class="on">!</span></li>
            <li class="symbol"><span class="off">2</span><span class="on">@@</span></li>
            <li class="symbol"><span class="off">3</span><span class="on">#</span></li>
            <li class="symbol"><span class="off">4</span><span class="on">$</span></li>
            <li class="symbol"><span class="off">5</span><span class="on">%</span></li>
            <li class="symbol"><span class="off">6</span><span class="on">^</span></li>
            <li class="symbol"><span class="off">7</span><span class="on">&amp;</span></li>
            <li class="symbol"><span class="off">8</span><span class="on">*</span></li>
            <li class="symbol"><span class="off">9</span><span class="on">(</span></li>
            <li class="symbol"><span class="off">0</span><span class="on">)</span></li>
            <li class="symbol"><span class="off">-</span><span class="on">_</span></li>
            <li class="symbol"><span class="off">=</span><span class="on">+</span></li>
            <li class="delete lastitem">DEL</li>
            <li class="tab">TAB</li>
            <li class="letter">q</li>
            <li class="letter">w</li>
            <li class="letter">e</li>
            <li class="letter">r</li>
            <li class="letter">t</li>
            <li class="letter">y</li>
            <li class="letter">u</li>
            <li class="letter">i</li>
            <li class="letter">o</li>
            <li class="letter">p</li>
            <li class="symbol"><span class="off">[</span><span class="on">{</span></li>
            <li class="symbol"><span class="off">]</span><span class="on">}</span></li>
            <li class="symbol lastitem"><span class="off">\</span><span class="on">|</span></li>
            <li class="capslock">CAPS</li>
            <li class="letter">a</li>
            <li class="letter">s</li>
            <li class="letter">d</li>
            <li class="letter">f</li>
            <li class="letter">g</li>
            <li class="letter">h</li>
            <li class="letter">j</li>
            <li class="letter">k</li>
            <li class="letter">l</li>
            <li class="symbol"><span class="off">;</span><span class="on">:</span></li>
            <li class="symbol"><span class="off">'</span><span class="on">&quot;</span></li>
            <li class="return lastitem">ENTER</li>
            <li class="left-shift">SHIFT</li>
            <li class="letter">z</li>
            <li class="letter">x</li>
            <li class="letter">c</li>
            <li class="letter">v</li>
            <li class="letter">b</li>
            <li class="letter">n</li>
            <li class="letter">m</li>
            <li class="symbol"><span class="off">,</span><span class="on">&lt;</span></li>
            <li class="symbol"><span class="off">.</span><span class="on">&gt;</span></li>
            <li class="symbol"><span class="off">/</span><span class="on">?</span></li>
            <li class="hide">HIDE</li>
            <li class="space lastitem">&nbsp;</li>
        </ul>

    </div>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

  </body>
</html>

<script>

    $( document ).ready(function() {

        // Hide Keyboard on load
        $('#keyboard li').hide();
    });


    $(function(){
    // Show Keyboard on click
    $('.tbx').click(function () {
        $('#keyboard li').show();

        inputBox = $(this).attr('id');
        //alert(inputBox);

        i = '#';
        GetId = i.concat(inputBox);
        $write = $(GetId),
        shift = false,
        capslock = false;

        $('#keyboard li').click(function () {
          $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;
          }

          // Hide Keyboard
          if ($this.hasClass('hide')) {
            $('#keyboard li').hide();
            return false;
          }

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

          // Delete
          if ($this.hasClass('delete')) {
            html = $write.html();
            $write.html(html.substr(0, html.length - 1));

            return false;
          }

          // Special characters
          if ($this.hasClass('symbol')) character = $('span:visible', $this).html();
          if ($this.hasClass('space')) character = ' ';
          if ($this.hasClass('tab')) character = "\t";
          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
          $write.html($write.html() + character);
        });
      });
    });


</script>'

2 个答案:

答案 0 :(得分:0)

每次点击输入时绑定点击#keyboard li。只需在点击之前添加unbind(),例如$('#keyboard li').unbind().click(function ()

答案 1 :(得分:0)

首先,我建议您修复代码,在需要时添加var并正确嵌套(如果确实需要)

$(document).ready(function () {
    var $write, shift, capslock; //declared globally

    // Hide Keyboard on load
    $('#keyboard li').hide();

    $('.tbx').click(function () {

        $('#keyboard li').show();


        var inputBox = $(this).attr('id'),
            i = '#',
            GetId = i.concat(inputBox);
        $write = $(GetId);
        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;
        }

        // Hide Keyboard
        if ($this.hasClass('hide')) {
            $('#keyboard li').hide();
            return false;
        }

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

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

            $write.html(html.substr(0, html.length - 1));

            return false;
        }

        // Special characters
        if ($this.hasClass('symbol')) character = $('span:visible', $this).html();
        if ($this.hasClass('space')) character = ' ';
        if ($this.hasClass('tab')) character = "\t";
        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
        $write.html($write.html() + character);


    });
});

然后,当您的所有代码都正确且可读时,您可以通过它查找错误