简体   繁体   English

在textarea中发送值

[英]Send value in textarea

Hi I was just wondering how to persist the data to another page. 嗨,我只是想知道如何将数据持久化到另一个页面。 So far I've found out that I need a form to send it in, Here's an example: 到目前为止,我发现我需要一个表格来发送它,这是一个示例:

HTML: HTML:

<form action="Result.html" method="get">
<textarea id="test" autofocus></textarea>
<p id="demo"></p>
<button onclick="myFunction()"></button>
</form>

jQuery: jQuery的:

function myFunction() {
   var x = document.getElementById("test").value;
   document.getElementById("demo").innerHTML = x;
}

This file that I'm using it's called Exercise1.html, I don't know if it helps or not but just in case it does. 我正在使用的该文件称为Exercise1.html,我不知道它是否有帮助,但以防万一。 I know HTML, CSS, jQuery and Javscript so I rather prefer that 我知道HTML,CSS,jQuery和Javscript,所以我更喜欢

Because loading a new page causes the JavaScript and HTML to be destroyed there is not way to preserve the value between page loads without some extra work. 由于加载新页面会导致JavaScript和HTML被破坏,因此无法在不进行任何额外工作的情况下保留页面加载之间的值。 There are essentially three options. 本质上有三种选择。

Server Script 服务器脚本

When the form sends data to a running server the server can take the value and inject it into the next page. 当表单将数据发送到运行中的服务器时,服务器可以获取该值并将其注入下一页。 Since this is a JavaScript question I will assume this is beyond the scope of the question. 由于这是一个JavaScript问题,因此我认为这不在问题范围内。

Cookies / localStorage Cookies /本地存储

Each page could look for a form of persistent storage on page load and populate the values. 每个页面可以在页面加载时寻找一种持久存储形式,并填充值。 Usually you can save data to localStorage or possibly a cookie. 通常,您可以将数据保存到localStorage或cookie。 Then on page load the JavaScript should load the value from storage and populate as needed. 然后在页面加载中,JavaScript应该从存储中加载值并根据需要填充。

Single Page App 单页应用

In the case of a Single page app the value would be in memory and you manipulate the DOM to swap out views There are many frameworks that offer things like routing to make it look like a new page even though it is still the same page. 对于单页应用程序,该值将存储在内存中,并且您操纵DOM换出视图 。许多框架都提供了路由之类的功能,以使其看起来像新页面,即使该页面仍然是同一页面。 Then you can populate values that way. 然后,您可以以这种方式填充值。

To explain in detail all options would be more then a single answer and more specific details should be searched and asked for. 为了详细解释所有选项,不只是一个答案,还应该搜索并要求更具体的细节。

In the exercise1.html: 在exercise1.html中:

<form id="form1" action="result.html" method="get">
    <textarea id="aboutme" name="aboutme" rows="10" cols="30"></textarea>
    <input type="submit" class="bottom" name="submit" id="submit" value="Sign up" >
</form>

In the result.html: 在result.html中:

<script>
var queryString = decodeURIComponent(window.location.search);
queryString = queryString.substring(1);
var queries = queryString.split("&");
for (var i = 0; i < queries.length; i++)
{
    var query = queries[i].split("=");
    document.write(query[1] + "<br>");
}
</script> 

If you like to persist your value to other pages on your webpage you can put the values in cookies: 如果您希望将值保留到网页上的其他页面,则可以将值放入cookie中:

// This saves your value
function saveField(){
    value = $('#test').val();
    document.cookie['myValue'] = value;
}

// This gets your value back on other pages
function loadField(){
    return document.cookie['myValue'];
}

More on how cookies work here 有关Cookie如何在此处工作的更多信息

Hi I just wanted to say that I found another way to do display what key that was pressed, if anyone else is interested. 嗨,我只是想说,如果其他人感兴趣,我发现了另一种方法来显示所按下的键。

Javascript: Javascript:

document.onkeypress = function(e) {
   e = e || window.event;
   var charCode = e.charCode || e.keyCode,
   character = String.fromCharCode(charCode);
   console.log(charCode);

   if(e.charCode == 98 || e.keyCode == 98) {
      document.write("B = " + charCode + "<br>");
   } else if(e.charcode == 114 || e.keyCode == 114) {
      document.write("R = " + charCode + "<br>");
   }
};

So when you press B or R it displays the keyCode or charCode and it works in Chrome, Explorer and in Firefox (but it's a little slow in firefox, but it works). 因此,当您按B或R时,它会显示keyCode或charCode,并且可以在Chrome,Explorer和Firefox中使用(但在Firefox中运行有些慢,但是可以使用)。

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

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