键盘在div中不起作用吗?

时间:2017-06-21 09:10:55

标签: javascript jquery html css html-table

我有类似这样的代码:

 <table>
    <tr>
        <td>
            <table class="table table-striped">
                <tbody>
                    <tr>
                    <td id="formulaWidget" class="formula" colspan="3">
                        <div id="formulaEditorContainer" class="FormulaEditor" style="height: 100%;">
                            <div class="head form-inline" style="margin-bottom: 5px;">
                                <input type="text" id="UxSearchAddHoc" placeholder="search" class="form-control" style="width: calc(100% - 45px);" />
                            </div>
                        </div>
                    </td>
                    </tr>
                </tbody>
            </table>
          </td>
    </tr>
</table>

这是我的js代码:

 $('#UxSearchAddHoc').keypress(function (event) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (Number(keycode) === 13) {
            event.preventDefault();
            event.stopPropagation();
        }
    }).autocomplete({
        source: function (request, response) {
            var a = request.term;
            response = getObjects();
        }
    });

keypress在表格中不起作用 - &gt; tr - &gt; td - &gt; div。我使用tabIndex但没有用。

我检查了document.activeElement和active元素:$('#UxSearchAddHoc')。然后我检查焦点与否,如下面的代码:

$('#UxSearchAddHoc').is(':focus')   //return false

我将.html移动到另一个地方并且文本按键工作但是如果在表中不起作用。

为什么会这样?我该怎么办? 请..

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
$('#UxSearchAddHoc').bind("keypress", function (event) {
    
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (Number(keycode) === 13) {
            alert("Enter Key pressed !");
            event.preventDefault();
            event.stopPropagation();
        }
        else
        {
            alert("Other Key pressed !"); 
        }
    }).autocomplete({
        source: function (request, response) {
            
        }
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<table>
    <tr>
        <td>
            <table class="table table-striped">
                <tbody>
                    <tr>
                    <td id="formulaWidget" class="formula" colspan="3">
                        <div id="formulaEditorContainer" class="FormulaEditor" style="height: 100%;">
                            <div class="head form-inline" style="margin-bottom: 5px;">
                                <input type="text" id="UxSearchAddHoc" placeholder="search" class="form-control" style="width: calc(100% - 45px);" />
                            </div>
                        </div>
                    </td>
                    </tr>
                </tbody>
            </table>
          </td>
    </tr>
</table>
&#13;
&#13;
&#13;