如何将光标集中在表单元素上
How to focus cursor on form element?
我想在表单元素中的空输入后添加光标。第一个空表单所在的位置
为此,我尝试将this.focus();
添加到validate()
函数中。但这并没有成功。
第二点——如何将brovser页面出现后的光标设置为第一个表单元素。我尝试将这种目标onLoad();
方法转化为body
。但这并没有成功。
代码:
<html>
<head>
<title>Form with check</title>
<script>
function validate() {
if(document.form1.yourname.value.length < 1) {
alert("Enter your name, please");
this.focus();
return false;
}
if(document.form1.adress.value.length < 3) {
alert("Enter your adress, please");
this.focus();
return false;
}
if(document.form1.phone.value.length < 3) {
alert("Enter your phone number, please");
this.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<h1>Form with check</h1>
<p>Input all data. When button Submit pushed data will be sent as message.</p>
<form name="form1" action="mailto:user@host.com" enctype="text/plain"
onSubmit="validate();">
<p><b>Name:</b><input type="text" length="20" name="yourname">
</p>
<p><b>Adress:</b><input type="text" length="20" name="adress">
</p>
<p><b>Phone:</b><input type="text" length="20" name="phone">
</p>
<input type="SUBMIT" value="Submit">
</form>
onLoad();
</body>
</html>
问题:
- 如何将此功能添加到表单
你忘了做了吗
onSubmit="return validate();" ?
用document.form1.yourname.focus();
替换this.focus()
这是重新处理的代码:
<html>
<head>
<title>Form with check</title>
<script>
function validate() {
if(document.form1.yourname.value.length < 1) {
alert("Enter your name, please");
document.form1.yourname.focus();
return false;
}
if(document.form1.adress.value.length < 3) {
alert("Enter your adress, please");
document.form1.adress.focus();
return false;
}
if(document.form1.phone.value.length < 3) {
alert("Enter your phone number, please");
document.form1.phone.focus();
return false;
}
document.getElementById("ff").submit();
return true;
}
</script>
</head>
<body >
<h1>Form with check</h1>
<p>Input all data. When button Submit pushed data will be sent as message.</p>
<form id="ff" name="form1" action="mailto:user@host.com" enctype="text/plain"
>
<p><b>Name:</b><input type="text" length="20" name="yourname">
</p>
<p><b>Adress:</b><input type="text" length="20" name="adress">
</p>
<p><b>Phone:</b><input type="text" length="20" name="phone">
</p>
<input type="button" value="Submit" onclick="validate();">
</form>
</body>
</html>
工作DEMO也
onSubmit="validate();"
在验证函数的上下文中,this
将是全局窗口对象。
要处理函数中的表单,根据您的代码,您可以使用document.form1
或通过id(或其他选择器(手动调用它,也可以将表单发送到函数:
<script>
function validate(sender) {
if(sender.yourname.value.length < 1) {
alert("Enter your name, please");
sender.focus();
return false;
}
if(sender.adress.value.length < 3) {
alert("Enter your adress, please");
sender.focus();
return false;
}
if(sender.phone.value.length < 3) {
alert("Enter your phone number, please");
sender.focus();
return false;
}
return true;
}
</script>
<form name="form1" action="mailto:user@host.com" enctype="text/plain" onSubmit="validate(this);">
<p>
<b>Name:</b><input type="text" length="20" name="yourname">
</p>
<p>
<b>Adress:</b><input type="text" length="20" name="adress">
</p>
<p>
<b>Phone:</b><input type="text" length="20" name="phone">
</p>
<input type="SUBMIT" value="Submit">
</form>
相关文章:
- Javascript更新孙窗口中的表单元素
- 阻止表单元素提交
- 使用带有.net autopostback的ryanfait.com自定义表单元素
- 使用javascript更改表单元素的ID值
- HTML5在提交并显示所需标签后显示隐藏的表单元素
- 为什么表单元素不应命名为submit
- 如何重置搜索表单中的特定表单元素
- 使用 ng-repeat访问 ng 表单元素/值
- 获取电子邮件附件的表单元素
- AJAX和php脚本后添加的表单元素未传输到$_POST
- 如何对未知表单元素进行表单验证
- 不显示为表单元素的图像输入按钮的动态创建和appendChild
- 在JavaScript中为表单元素生成键值对
- 表单元素上的jQuery focusout事件即使在单击子元素时也会被触发
- 如何在 OnKeyPress 事件后获取输入表单元素的值
- 表单元素值更改后的角度 JS 验证
- 使用 javascript 中的表单元素构建一个 xml 字符串
- 如何以角度访问表单元素的$valid
- 如果选择了多个选项,则使用 jQuery 显示隐藏的表单元素
- JSON 填充的表单元素不使用 angularJs 过滤器进行过滤