HTML 表单 - 不要写入默认值
我在网上找到了大量的解决方案,用于在聚焦字段时清除字段中的默认文本,然后在用户退出字段而不输入任何内容时替换它。我最终采用的是来自 Electric Toolbox (http://www.electrictoolbox.com/jquery-change-default-value-on-focus/) 的解决方案:
$('.default-value').each(function() {
var default_value = this.value;
$(this).focus(function() {
if(this.value == default_value) {
this.value = '';
}
});
$(this).blur(function() {
if(this.value == '') {
this.value = default_value;
}
});
});
这工作得很好;当聚焦时,默认值类的每个文本输入字段都会切换为空白字段。当我离开字段时,如果未输入任何内容,则返回默认值,或者如果输入了某些内容,则该数据保留在字段中。到目前为止,一切都很好。
然而这里有一个问题;当用户提交表单时,该字段的默认数据将被写入数据库。由于并非所有字段都是必需的,因此我们有几个用户可能不会选择填写的字段。这意味着我们有一个最终数据列表,其中有很多人的地址为:“John Sample,街道地址,地址 2,城市州邮政编码”。
我希望能够告诉表单如果其中的信息与默认值匹配,则不要提交字段。我不知道 javascript - 我只是拼凑我能找到的代码 - 所以我确信我的语法错误,但理想情况下这会是这样的:
onsubmit
if address1 = "Street Address"
then address1 = ""
有没有任何相当干净和简单的方法来做到这一点? (另外,如果你能像在和最近遭受脑外伤的人交谈一样解释它,那就太好了。)
I've found a ton of solutions online for clearing default text out of a field when the field is focused on, then replacing it if the user exits the field without entering anything. The one I finally went with was this solution from Electric Toolbox (http://www.electrictoolbox.com/jquery-change-default-value-on-focus/):
$('.default-value').each(function() {
var default_value = this.value;
$(this).focus(function() {
if(this.value == default_value) {
this.value = '';
}
});
$(this).blur(function() {
if(this.value == '') {
this.value = default_value;
}
});
});
This works just fine; every text input field of class default-value switches to a blank field when focused on. When I leave the field, either the default value returns if nothing was entered, or if something was entered, that data stays in the field. So far so good.
There's a catch here however; the data in the default of the field is being written to the database when the user submits the form. Since not all of our fields are required, we have several which a user may not choose to fill out. This means we have a final data list with a lot of people with an address of: "John Sample, Street Address, Address 2, City State Zip."
What I would love is to be able to tell the form not to submit fields if the information in them matches the default. I don't know javascript - I'm just cobbling together code that I can find - so I'm sure I have the syntax wrong, but ideally this would be something like:
onsubmit
if address1 = "Street Address"
then address1 = ""
Is there any reasonably clean and simple way to do this? (Also, if you could explain it like you're talking to someone who recently suffered a traumatic brain injury, that would be good too.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你知道占位符html5属性吗?
Do you know placeholder html5 attribute ?
在提交处理程序中,迭代每个字段以查看字段值是否等于标签,在这种情况下设置为空。
In your submit handler, iterate over each field to see if field values are equal to the label, in which case set to empty.
您可以简单地使用占位符属性!
http://jsfiddle.net/3efja/
You could simply use the placeholder attribute!
http://jsfiddle.net/3efja/
语法可以是
The syntax could be
加载时,您应该创建一个默认值的数组,然后在提交时,循环遍历每个字段并查看它是否仍然具有默认值,如果是,则将其设置为空白。
唯一的问题是他们的名字是否真的是“名字”。
On load, you should create an array of the default values, then on submit, loop through each field and see if it still have the default, and if so, set it to blank.
The only catch is if their name really is "First Name".