gpt4 book ai didi

javascript - 如何在文本字段中设置焦点

转载 作者:行者123 更新时间:2023-11-30 10:35:49 24 4
gpt4 key购买 nike

我有一个注册表单。我想在客户端本身实现一些验证。我正在使用按钮提交表单。

<button onclick="check_fields();"></button>

function check_fields() {
if(document.contact_form.name.value=='')
document.contact_form.name.focus();
else
document.getElementById("contact_form").submit();
}

焦点没有发生,而是表单成功提交。请帮忙。

最佳答案

最好的方法是使用 id

比方说,您以这种方式定义了文本字段:

<input type="text" name="contact" id="contact" />

在您的 JavaScript 中,最好这样给出:

document.getElementById("contact").focus();

关于javascript - 如何在文本字段中设置焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14092055/

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