使用 jQuery 进行“自动填充”第二个文本框,基于第一个文本框的输入
好吧,基本上我想做的是使用文本框的值(在大多数情况下是自动完成的)来填充另一个文本框,其中包含从数据库中提取的数据。
用户将在一个框中输入姓名,一旦该输入失去焦点,第二个框中就会自动填充该姓名的关联电子邮件地址。
我见过一些不同的 jQuery 插件,它们与我想要的类似,但仅适用于选择(我不想要它,因为该名称可能是要添加的新名称)。
我知道有人会说这样的话:“好吧,为什么不从数据库中提取姓名和电子邮件地址并在页面加载时填充表单。”答案是:因为名称将被动态添加(当您添加新订单时,请按照订单跟踪软件的思路思考)。
我使用 PHP、MySQL,当然还有 jQuery。
提前致谢。
Alright, so basically what I'm trying to do is use the value (which in most cases is autocompleted) of a text box to fill in another text box with data pulled from the database.
The user will input a name in one box and once that input loses focus, a second is auto populated with the name's associated email address.
I've seen a few different jQuery plugins that are similar to what I want, but only work for selects (which I do not want since there is the possibility that the name will be a new one to be added).
I know someone is going to say something along the lines of: "Well, why don't you just pull the name and email address from the database and populate the form on page load." The answer: because the name will be added on the fly (think along the lines of an order tracking software, when you add a new order).
I'm using PHP, MySQL, and of course jQuery.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不需要插件来做到这一点,只需使用模糊事件调用 $.post
在你的 php 文件中,你将能够使用
$_POST["name"]
获取名称要将电子邮件发送回 javascript,只需回显它。如果您需要加载的数据不仅仅是字符串,您应该使用 json_encode() 来编码数组
并将您的 post 调用更改为:
You don't need a plugin to do that, just call $.post with the blur event
In you php file, you'll be able to get the name with
$_POST["name"]
and to send the email back to the javascript, just echo itIf you need to load more data than only a string, you should use json_encode() to encode an array
And change your post call to this:
你可以做这样的事情...
从上面的 jQuery 代码中,你可以在 php 中执行
$_GET['foo']
...并且你应该echo
第二个输入框需要数据......you could do something like this...
from that above codes of jQuery, you could do
$_GET['foo']
in php... and you shouldecho
the needed data for the second inputbox....客户端可能如下所示:
当“名称”字段失去焦点时,向 Lookupemail.php 发送请求,并将参数“名称”设置为“名称”字段。当请求返回时,将其放入“电子邮件”字段中。
Here's what it might look like client side:
When the 'name' field loses focus send a request to lookupemail.php with the parameter 'name' set to the 'name' field. When the request is returned, put it in the 'email' field.