如何传递来自<a> to textarea on another page?

时间:2018-10-10 09:47:56

标签: javascript jquery html forms product

What I want is pretty much the most basic Add product to enquiries you can get.

On my product page there will be a button add to enquiries.

Now how can I pass just that product name when clicking on the add to enquries button to the enquiries page. That name must then be entered into a textarea field.

I would prefer to do this using Javascript or JQuery if I can.

Any help would be greatly appreciated.

3 个答案:

答案 0 :(得分:1)

您可以使用localstorage。例如,在产品页面中,单击按钮后,可以将其存储在本地存储中,如下所示:

function addToEnquiries(){
let productName= document.getElementById("productName").value;
localStorage.setItem("product",productName);
}

然后在“查询”页面的页面加载中,您可以将其检索为:

function onPageLoad(){
console.log("Product name is ",localStorage.getItem("product"));
}

答案 1 :(得分:0)

每个作者的请求是在网址中使用参数的一种方法。

来自here的解决方案。

getInputStream()

假设网址为var getUrlParameter = function getUrlParameter(sParam) { var sPageURL = decodeURIComponent(window.location.search.substring(1)), sURLVariables = sPageURL.split('&'), sParameterName, i; for (i = 0; i < sURLVariables.length; i++) { sParameterName = sURLVariables[i].split('='); if (sParameterName[0] === sParam) { return sParameterName[1] === undefined ? true : sParameterName[1]; } } }; ,您可以获取类似的参数。

http://dummy.com/?textareaContent=loremispum

但是要小心,如果要将文本添加到链接中,则需要先对其进行编码,然后再对其进行解码。以下是一些文档:https://www.w3schools.com/jsref/jsref_encodeURI.asp

我仍然建议使用本地存储更容易。

答案 2 :(得分:0)

只需添加少量产品,只需修改一下编码飞溅的答案即可

function addToEnquiries(){
    var productName = document.getElementById("productName").value;
    var product = localStorage.getItem('product');
    if(!product)
    {
        product = new Array();
    }
        product.push(productName);
        localStorage.setItem("product",product);
    }
}