gpt4 book ai didi

javascript - 更改单选后如何聚焦于输入字段?

转载 作者:行者123 更新时间:2023-12-03 03:10:32 25 4
gpt4 key购买 nike

在更改单选按钮后,我尝试设置输入字段的焦点。更改操作被触发,因为正在显示弹出警报。但焦点未设置在输入字段上。我的代码有什么问题?

感谢您的帮助!

$("input[name=options]").change(function () {
alert(this.value);
document.getElementById("input1").focus();
});
<!doctype html>
<html lang="en">
<head>
<title>Hello, world!</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
</head>

<body>
<div class="container">
<div class="row justify-content-center">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="options" value="x1" id="option1" autocomplete="off" checked> Radio 1
</label>
<label class="btn btn-primary">
<input type="radio" name="options" value="x2" id="option2" autocomplete="off"> Radio 2
</label>
<label class="btn btn-primary">
<input type="radio" name="options" value="x3" id="option3" autocomplete="off"> Radio 3
</label>
</div>
</div>
<br>
<div class="row justify-content-center">
<form>
<div class="input-group input-group-lg">
<input type="number" class="form-control" id="input1" autocomplete="off">
<span class="input-group-btn">
<button type="submit" class="btn btn-primary btn-custom">
Go!
</button>
</span>
</div>
</form>
</div> <!-- row -->
</div>

<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>

</body>
</html>

最佳答案

使用以下内容。

$("input[name=options]").change(function () {
window.setTimeout(function () {
document.getElementById("input1").focus();
}, 0);
});

如果任何浏览器不支持,请将超时值从 0 增加到 50 或 100

也不要使用警报。警报将使您从文本框中集中注意力。

关于javascript - 更改单选后如何聚焦于输入字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46970875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com