简体   繁体   English

从HTML表单获取值作为JavaScript变量

[英]Getting values from html form as JavaScript variables

i want to capture input variables sent via form from one page to another.html page and obtain these values as JavaScript variables. 我想捕获通过表单从一个页面发送到another.html页面的输入变量,并获取这些值作为JavaScript变量。 Can this be done. 可以做到这一点。

This is my form; 这是我的表格;

<form action='another.html' id='form' data-ajax='false'>
<input id='sent_by' value='tom'/>
<input type='submit' value='Submit'/>
</form>

And in my another.html i tried to get the values as; 在我的another.html中,我尝试将值设为:

var sent_by = $.session.get("sent_by");

But i am not able to get the values. 但是我无法获得这些价值。 All help is appreciated. 感谢所有帮助。

You can use the browser's localStorage to achieve this. 您可以使用浏览器的localStorage实现此目的。 Before submitting and going to the other page store all the values of the form in the localStorage and you can access it on the other page: 在提交并转到另一页面之前,将表单的所有值存储在localStorage中,您可以在另一页面上访问它:

Page1.html Page1.html

Field Name = "name" <input type="text" name="name" id="name" /> 字段名称=“名称” <input type="text" name="name" id="name" />

Read the value and store it to localStorage 读取值并将其存储到localStorage

localStorage.setItem('name', document.getElementById('name').value);

and so on. 等等。 You can make a function in JavaScript that saves all the fields of the form in localStorage on / before submit. 您可以在JavaScript中创建一个函数,以便在提交之前将表单的所有字段保存在localStorage上。

To read these values on the other page: 要在另一页上读取这些值:

Page2.html Value stored for key name can be get using the following JavaScript: Page2.html键名存储的值可以使用以下JavaScript获取:

localStorage.getItem("name");

NOTE 注意

the page1.html and page2.html should be in the same domain for you to access the localStorage. page1.html和page2.html应该位于同一域中,以便您访问localStorage。

Read more about localstorage at https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage https://developer.mozilla.org/zh-CN/docs/Web/API/Storage/LocalStorage上了解有关本地存储的更多信息

Have you tried using Window.localStorage ? 您是否尝试过使用Window.localStorage It's similar to sessionStorage but with no expiration date. 它类似于sessionStorage但是没有到期日期。 So, be sure to clear it once the browsing session ends. 因此,请确保在浏览会话结束后将其清除。

I think the answer provided by @Shakti Phartiyal is probably the most practical and a sweet trick I might add. 我认为@Shakti Phartiyal提供的答案可能是最实用的,也是我可能添加的一个甜头。 The main reason I'm writing this post is because I had also taken a long path of using javascript to pass along this kind of information. 我写这篇文章的主要原因是因为我也花了很长时间使用javascript传递此类信息。 It resulted in me bewildered at how powerful PHP can be for some tasks that you dedicated javascript to do. 这让我感到困惑,因为您专用的javascript可以完成某些任务的PHP功能多么强大。 (Yea I know some javascript wizards out there can do everything with it, I'm just talking about basic programming tho). (是的,我知道有些JavaScript向导可以用它完成所有工作,我只是在谈论基本编程。) So if you wondered what the PHP way of passing this around: 因此,如果您想知道PHP传递这种方式的方式:

Your modified html form: 您修改后的html表单:

<form action='another.html' id='form' data-ajax='false' method='post'>
<input id='sent_by' value='tom' name='uname'/>
<input type='submit' value='Submit'/>
</form>

and then in "another.php" you would have this to retrieve the input from the form: 然后在“ another.php”中,您可以从表单中检索输入:

<?php
$uname= htmlspecialchars($_POST['uname'];
?>

Great. 大。 But how to make this php variable into javascript? 但是如何使这个php变量变成javascript? Ah, the fun part. 啊,有趣的部分。 You're going to write javascript to your webpage with php - you do something like this: 您将使用php将javascript写入网页-您可以执行以下操作:

var uname = "<?php echo $uname; ?>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM