【发布时间】:2015-06-01 10:39:50
【问题描述】:
我开发了一个 html 联系表。如何捕获使用 JavaScript 在表单中输入的数据? (我不能使用 jQuery)?我想我需要使用document.GetElementById(),但是如何?当用户离开字段、单选按钮或复选框时,我是否需要使用诸如onBlur 之类的事件来捕获它?
/*Borders of fields for validation and indication*/
input:invalid{
box-shadow: 0 0 2px .5px red;
}
textarea:invalid{
box-shadow: 0 0 2px .5px red;
}
/*Spacing around fields this is in place of <br>*/
label{
display: block;
padding-top: 5px
}
<!DOCTYPE html>
<html>
<head>
<title>Contact Me</title>
<link rel="stylesheet" type="text/css" href="contactform_Lab8.css">
</head>
<body>
<form id="contactus">
<fieldset>
<label for="name">Name:</label>
<input id="name" type="text" name="name" autofocus required>
<label for="email">Email:</label>
<input id="email" type="email" name="email" required>
<label for="phone">Phone:</label>
<input id="phone" type="tel" name="phone" required>
<label for="status">Status:
<select id="status" name="status" required>
<option value="client">Client</option>
<option value="partner">Partner</option>
<option value="vendor">Vendor</option>
</select>
</label>
<label for="subscribe">
<input id="subscribe" type="checkbox" name="subscribe" value="check" checked>
Send me your newsletter</label>
<label for="sales">
<label for="support">
<input id="slsSupport" type="radio" name="slsSupport" value="sales" checked>Sales
<input id="slsSupport" type="radio" name="slsSupport" value="support">Support
</label>
</label>
<label for="msg">Message:</label>
<textarea id="msg" name="msg" rows="10" cols="30" required></textarea>
</fieldset>
<fieldset>
<button type="submit">Send</button>
<button type="reset">Reset</button>
</fieldset>
</form>
<script src="contactform_Lab8.js"></script>
</body>
</html>
【问题讨论】:
-
这是您要找的吗? - stackoverflow.com/questions/11563638/…
-
在询问之前总是谷歌:)
-
@wavemode 这就是他所知道的部分。他不明白的是如何使用事件处理程序运行它。
-
不要使用getelementById,而是使用jQuery,这将提供更好的跨浏览器兼容性。要了解使用 jQuery 获取值的想法,请查看 .val() ,其中包含示例代码。您还可以使用 jQuery 轻松处理事件。
-
为了更好的 UI,我会选择
change和input事件而不是blur等。
标签: javascript html css forms