使用 jquery 进行预填充(使其可扩展)
我下面有 jquery ,它基本上预先填充了一个文本框。 (它实际上填充了文本框上方的跨度,然后将其定位在文本框上方。然后,当单击该跨度时,它会隐藏它并聚焦于文本框。)这样我就不必添加预先填充的值到表单字段中,因此如果用户提交,则不会提交填充的值。现在这似乎有效,但问题是我希望页面上的文本框具有所有具有不同预填充值的功能。我正在尝试找出一种方法来实现这种规模,所以我基本上只需要一个函数,并且根据文本框的 ID,它会将不同的数据填充到上面的范围中。任何帮助都会很棒!
仅适用于 1 个文本框的 jquery 代码:(我真的不想为每个文本框一遍又一遍地编写此函数
if($("#workphone").val().length == 0)
{
var overlayCellPhone = $("<span class='prepopulation'>1234567890</span>");
$("#workphone").parent().prepend(overlayCellPhone);
$(".prepopulation").click(function(){
$("#workphone").focus()
$(".prepopulation").hide();
});
$("#workphone").blur(function(){
if($("#workphone").val().length == 0)
{
$(".prepopulation").show();
}
});
}
I have the jquery below which basically pre-populates a text box. (It actually populates a span above the text box which is then positioned over the text box. Then when the span is clicked, it hides it and focuses on the text box.) This way I don't have to add the pre-populated values into the form field, so if the user submits it won't submit the populated values with it. Right now this seems to be working, but the problem is I have about text boxes on the page that I want to have this functionality all with different pre-populated values. I'm trying to figure out a way to make this scale, so I just need basically one function, and depending on the ID of the text box it will populate different data into the span above. Any help would be great!
Working jquery code for only 1 text box: (I really don't want to write this function over and over again for each text box
if($("#workphone").val().length == 0)
{
var overlayCellPhone = $("<span class='prepopulation'>1234567890</span>");
$("#workphone").parent().prepend(overlayCellPhone);
$(".prepopulation").click(function(){
$("#workphone").focus()
$(".prepopulation").hide();
});
$("#workphone").blur(function(){
if($("#workphone").val().length == 0)
{
$(".prepopulation").show();
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它应该看起来像这样:
It should look like this:
HTML5 允许您为输入字段设置占位符文本,方法是指定如下内容:
之后,使用 < a href="https://github.com/mathiasbynens/Placeholder-jQuery-Plugin" rel="nofollow">一个 jQuery 插件,用于在旧版浏览器上启用
占位符
。HTML5 allows you to set placeholder text for input fields, by specifying something like:
After that, use a jQuery plugin to enable
placeholder
s on older browsers.