Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将光标设置为html文本输入字段?_Javascript_Html_Forms - Fatal编程技术网

Javascript 如何将光标设置为html文本输入字段?

Javascript 如何将光标设置为html文本输入字段?,javascript,html,forms,Javascript,Html,Forms,对于Flask应用程序,我试图将光标设置为表单中的输入文本字段。表单如下所示: <form method="get" autocomplete="off"> <div class="row"> <div class="four columns"> <label for="from-currency">Exchange:</label>

对于Flask应用程序,我试图将光标设置为表单中的输入文本字段。表单如下所示:

<form method="get" autocomplete="off">
        <div class="row">
            <div class="four columns">
                <label for="from-currency">Exchange:</label>
                <input type="text" placeholder="currency to exchange from" value="{{ from_curr }}" name="from_currency" id="from-currency" class="input-field"/>
            </div>
            <div class="four columns">
                <label for="to-currency">To:</label>
                <input type="text" placeholder="currency to exchange to" value="{{ to_curr }}" name="to_currency" id="to-currency" class="input-field"/>
            </div>
            <div class="four columns">
                <label for="calculate-button">&nbsp;</label>
                <input type="submit" value="Calculate" id="calculate-button" class="input-field">
            </div>
        </div>
    </form>

交换:
致:
我尝试使用JavaScript(element.focus();),但它没有将光标移动到我的输入字段中

<script>
    function submit_param(inp) {
        inp.addEventListener("input", function(event) {
            var val = this.value;
            var urlParams = new URLSearchParams(window.location.search);
            urlParams.set(inp.name, val);
            var queryString = urlParams.toString();
            history.pushState({}, "", "?"+queryString);
            location.reload();
            inp.focus();
        });
    }
    submit_param(document.getElementById("from-currency"));
    submit_param(document.getElementById("to-currency"));
</script>

函数提交参数(inp){
inp.addEventListener(“输入”,函数(事件){
var val=该值;
var urlParams=新的URLSearchParams(window.location.search);
urlParams.set(inp.name,val);
var queryString=urlParams.toString();
pushState({},“,?”+queryString);
location.reload();
inp.focus();
});
}
提交参数(document.getElementById(“来自货币”);
提交参数(document.getElementById(“到货币”);
我做错了什么,或者如何将光标移回脚本块末尾的输入字段

如何将光标设置为html文本输入字段


“自动聚焦”属性是一个布尔属性

当存在时,它指定当页面加载时,元素应自动获得焦点


希望这有帮助。祝你好运。

我认为问题在于你调用了
inp.focus()重新加载/刷新页面后。
<input type="text" autofocus>