[英]Javascript to add text into textarea then save textarea as cookie
My client want a site displaying his products online, but he didnt want an ecommerce site since its the number of product he has is very low. 我的客户想要一个在线显示其产品的站点,但是他不想要一个电子商务站点,因为该站点拥有的产品数量很少。
I want to make a button on each product that when a user click will store it inside a cookie. 我想在每个产品上创建一个按钮,当用户单击该按钮时会将其存储在Cookie中。 What i come up with is that i make a hidden textarea inside the page and when the user click the order button on each product, it will be added to the text area and saved into a cookie simultaneously.
我想到的是,我在页面内创建了一个隐藏的文本区域,当用户单击每种产品上的订购按钮时,它将被添加到文本区域并同时保存到Cookie中。 Then when the user go to the order page, the orders will be loaded into a textarea.
然后,当用户转到订单页面时,订单将被加载到文本区域中。
The javascript part i found on a page online. 我在在线页面上找到了javascript部分。 Below is my code;
下面是我的代码;
<div id="hide" style="display: none;">
<textarea id="myContent2" name="myContent2"></textarea>
<a onClick="addContent('myDiv2', document.getElementById('myContent2').value); setCookie('content', document.getElementById('myContent2').value, 7);">
<br><br>
<div id="myDiv2" style="font-weight: bold;"></div>
</div>
<script type="text/javascript">
function addContent(divName, content) {
document.getElementById(divName).innerHTML = content;
}
</script>
<script type="text/javascript">
function setCookie(c_name,value,expiredays) {
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name) {
if (document.cookie.length>0) {
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1) {
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
var cookieContent = "Welcome back " + unescape(document.cookie.substring(c_start,c_end));
document.getElementById('myDiv2').innerHTML = cookieContent;
}
}
}
getCookie('content');
</script>
this is where the user click on the order button at the product page 这是用户单击产品页面上的订购按钮的地方
<a onClick="addContent('myDiv2', document.myContent2.append("Product No 1") ); setCookie('content', document.getElementById('myContent2').value, 7);">
the thing is, when the user click the button, it wont send anything to the textarea and at the same time doesnt update the cookie. 问题是,当用户单击按钮时,它不会将任何内容发送到textarea,同时不会更新cookie。 How can i fix this and how can i make sure that each time the user click order button, it will add new data into the textarea instead of erasing over it.
我该如何解决此问题,以及如何确保每次用户单击“订购”按钮时,它都会将新数据添加到文本区域中,而不是擦除它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.