获取 HTML 文本输入的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8808519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting value of HTML text input
提问by Kevin
Say I have an HTML form like this to collect an email from a website visitor:
假设我有一个这样的 HTML 表单来收集来自网站访问者的电子邮件:
<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?
现在,当按下提交按钮时,我可以使用 $_POST["email"] 获取 handle_email.php 中电子邮件字段的值。但是说我想在其他地方使用电子邮件文本输入的值。是否可以在表单处理的“外部”获取电子邮件值?
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:
更新:感谢大家的快速回复。我想要做的是让电子邮箱做双重任务。例如,我在上面的代码中将提交按钮重命名为“Newsletter”。当该按钮被按下时,handle_email.php 脚本将只为用户注册一个时事通讯。但是,我在同一页面上还有一个“注册”链接,可以将访问者重定向到一个注册表单,如果他们愿意,他们可以在其中输入许多其他信息。现在,如果用户碰巧在电子邮件文本框中输入了数据,我想将其发送到注册页面,以便可以预先填充该字段。例如:
<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.
我很抱歉在我的原始帖子中不够清楚。
回答by Nick Tiberi
If you want to use the value of the email input somewhere else on the same page, for example to do some sort of validation, you could use JavaScript. First I would assign an "id" attribute to your email textbox:
如果您想在同一页面上的其他地方使用电子邮件输入的值,例如进行某种验证,您可以使用 JavaScript。首先,我会为您的电子邮件文本框分配一个“id”属性:
<input type="text" name="email" id="email"/>
and then I would retrieve the value with JavaScript:
然后我会用 JavaScript 检索值:
var email = document.getElementById('email').value;
From there, you can do additional processing on the value of 'email'.
从那里,您可以对“电子邮件”的值进行额外处理。
回答by j08691
See my jsFiddle here: http://jsfiddle.net/fuDBL/
在此处查看我的 jsFiddle: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.
每当您更改电子邮件字段时,链接都会自动更新。这需要少量的jQuery。所以现在您的表单将根据需要工作,但您的链接将动态更新,以便当有人点击它时,它包含他们在电子邮件字段中输入的内容。您应该验证接收页面上的输入。
$('input[name="email"]').change(function(){
$('#regLink').attr('href')+$('input[name="email"]').val();
});
回答by Sagar Patil
Depends on where you want to use the email. If it's on the client side, without sending it to a PHP script, JQuery (or javascript) can do the trick.
取决于您想在哪里使用电子邮件。如果它在客户端,无需将其发送到 PHP 脚本,JQuery(或 javascript)就可以解决问题。
I've created a fiddle to explain the same - http://jsfiddle.net/qHcpR/
我创建了一个小提琴来解释相同的 - http://jsfiddle.net/qHcpR/
It has an alert which goes off on load and when you click the textbox itself.
它有一个警报,它会在加载时和当您单击文本框本身时发出。
回答by Michael Sazonov
If your page is refreshed on submitting - yes, but only through the querystring: http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx(You must use method "GET" then). Else, you can return its value from the php script.
如果您的页面在提交时刷新 - 是的,但只能通过查询字符串:http: //www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx(您必须使用方法然后“获取”)。否则,您可以从 php 脚本返回其值。
回答by Dipu Raj
Yes, you can use jQuery to make this done, the idea is
是的,您可以使用 jQuery 来完成此操作,这个想法是
Use a hidden value in your form, and copy the value from external text box to this hidden value just before submitting the form.
在表单中使用隐藏值,并在提交表单之前将外部文本框中的值复制到此隐藏值。
<form name="input" action="handle_email.php" method="post">
<input type="hidden" name="email" id="email" />
<input type="submit" value="Submit" />
</form>
<script>
$("form").submit(function() {
var emailFromOtherTextBox = $("#email_textbox").val();
$("#email").val(emailFromOtherTextBox );
return true;
});
</script>
also see http://api.jquery.com/submit/
回答by REL
just use easy javascript:
只需使用简单的javascript:
<form name="input" action="handle_email.php" method="post">
Email: <input type="text" name="email" />
<input type="submit" value="Newsletter" />
</form>
<a href="javascript:var the_mail = document.getElementByName('email').value; window.location = 'http://mywebsite.com/register?user_email=' + the_email;">Register</a>
you can also change the type of the email input like:
您还可以更改电子邮件输入的类型,例如: