发送没有提交按钮的表单

Cal*_*ann 2 html forms send

我进行分页,我想在表单编号的文本字段中插入一个页面,表单将在用户按下 Enter 键或将光标移出文本字段时发送。

<form method="GET" action="/site">
<input type="text" name="page" />
</form>
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Dha*_*man 5

你可以用纯 Javascipt 做到这一点。这是一个粗略的伪代码,可以让您在 IIFE 中入门:

<form method="GET" action="/site">
    <input type="text" name="page" id="yourInput" />
</form>

<script>
(function() {
    var el = document.getElementById('yourInput'); //Get your input by ID
    el.onblur = function() { // when focus lost...
        this.form.submit(); // submit the form
    }
})();
</script>
Run Code Online (Sandbox Code Playgroud)