【问题标题】:submit a form on pressing of enter and show the results in a modal without reloading [duplicate]按 Enter 提交表单并以模式显示结果而无需重新加载[重复]
【发布时间】:2018-04-30 02:21:42
【问题描述】:

所以,这是我的表格……它没有被<form></form> 标签包围。

<input id="query" name="query" style="font-size: 18pt" id="text" data-email="required" type="text" placeholder="Search Here from <?php echo $row['no']."+"; ?> ..." class="extra-big-input border-none">
<div class="input-group-btn">
    <button onclick="search()" href="#result-modal" class="btn btn-large bg-white text-medium-gray btn-rounded btn-dark-gray popup-with-zoom-anim wow" data-wow-delay="0.6s"><i class="ti-search icon-small tz-icon-color no-margin position-raltive top-2"></i> Search</button>
</div>

点击按钮时,search() 被调用

<script>
        function search()
        {
            document.getElementById('queryresult').innerHTML = "<img src='images/loading.svg'>";
            var query=document.getElementById('query').value;
            $.ajax({
            method: 'POST',
            url: 'results.php',
            data: {
                query: query
            },
            success: function(data) {
                document.getElementById('queryresult').innerHTML = data;
            }
            })
        }
    </script>

这是我的问题...我如何更改此代码,以便在按 Enter 键时也可以调用 search()。??

注意:-

如果查询输入为空,则不应调用该函数[对空查询调用它没有好处]

单击按钮也会显示模态[显示结果的位置],所以我如何调用 search() 并在按下回车键时显示模态??

【问题讨论】:

  • 放入表单中,使用表单提交事件。如果当您点击输入时表单中存在提交按钮,浏览器默认提交表单。只需防止表单提交事件并做你需要的事情

标签: javascript jquery html ajax forms


【解决方案1】:

试试这个:

window.onkeyup = function(e) {
search()
console.log(e)
}

如果你只想要某个特定的键来执行搜索,请使用 e.keyCode

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多