获取HTML文本输入的价值 [英] Getting value of HTML text input

查看:109
本文介绍了获取HTML文本输入的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的HTML表单来收集来自网站访问者的电子邮件:

 < form name =输入action =handle_email.phpmethod =post> 
电子邮件地址:< input type =textname =email/>
< input type =submitvalue =Newsletter/>
< / form>

现在,当按下submit按钮时,我可以通过handle_email.php获得email字段的值使用$ _POST [email]。但是,说我想使用其他地方的电子邮件文本输入的价值。是否有可能获得电子邮件的价值外的表单处理?



更新:感谢大家的快速答复。我想要做的是让电子邮箱执行双重任务。例如,我已经在上面的代码中将提交按钮重命名为简报。当按下该按钮时,handle_email.php脚本将仅向用户注册简报。不过,我也在同一页面上有一个注册链接,将访问者重定向到注册表单,如果他们愿意,他们可以输入许多其他信息。现在,如果用户碰巧在电子邮件文本框中输入了数据,我想将它发送到注册页面,以便字段可以预先填充。例如:

 < a href =http://mywebsite.com/register?user_email=[我怎么能把电子邮件放在这里?]/>注册< / a> 

足够在我原来的职位。

解决方案

请参阅我的jsFiddle这里: http://jsfiddle.net/fuDBL/



每当您更改电子邮件字段时,链接都会自动更新。这需要少量的jQuery,所以现在你的表单会根据需要工作,但是你的链接会被动态地更新,这样当有人点击它时,它会包含他们在电子邮件字段中输入的内容,你应该验证接收时的输入

  $('input [name =email]')。change(function(){
$('#regLink')。attr('href')+ $('input [name =email]')。val();
});


Say I have an HTML form like this to collect an email from a website visitor:

<form name="input" action="handle_email.php" method="post">
Email: <input type="text" name="email" />
<input type="submit" value="Newsletter" />
</form> 

Now when the submit button is pressed I can get the value of the email field in handle_email.php by using $_POST["email"]. But say I'd like to use the value of the email text input elsewhere. Is it possible to get the email value "outside" of the form processing?

UPDATE: Thanks to everyone for their quick replies. What I'm trying to do is get the email box to do double-duty. For example, I've renamed the submit button to "Newsletter" in the above code. When that button is pressed the handle_email.php script will just sign up the user for a newsletter. However, I also have a "Register" link on the same page that redirects the visitor to a registration form where they can enter lots of other info if they like. Now, if the user happened to have entered data in the email text box I'd like to send it along to the registration page so that field can be pre-populated. For example:

<a href="http://mywebsite.com/register?user_email="[how can I put the email here?]"/>Register</a>

My apologies for not being clear enough in my original post.

解决方案

See my jsFiddle here: http://jsfiddle.net/fuDBL/

Whenever you change the email field, the link is updated automatically. This requires a small amount of jQuery. So now your form will work as needed, but your link will be updated dynamically so that when someone clicks on it, it contains what they entered in the email field. You should validate the input on the receiving page.

$('input[name="email"]').change(function(){
  $('#regLink').attr('href')+$('input[name="email"]').val();
});

这篇关于获取HTML文本输入的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆