我用JS编写了一些脚本并将其连接到html。
这两段代码是相同的,但对于textarea“名称”有效,而对于textarea“约”有效。请帮帮我!
HTML(不是整个文档):
<div>
Назва:
<textarea type="text" class="form-control" class="name" id="name"></textarea>
<span class="namme"></span>
</div>
<div>
Опис товару:
<textarea type="text" class="form-control" class='about' id='about'></textarea>
<span class="abbout"></span>
</div>
JS:
jQuery(document).ready(function($){
$("#name").change(function(){
var name1 = $("textarea#name").val();
$(this).next('.namme').text("Запам'ятовано");
createCookie("namme", name1, "1");
});
$('.name').trigger('change');
$('#about').change(function(){
var about1 = $('#about').val();
$(this).next('.abbout').text("Запам'ятовано");
createCookie("abbout", about1, "1");
});
$('.about').trigger('change');
});
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else {
expires = "";
}
document.cookie = encodeURIComponent(name) + "=" +
encodeURIComponent(value) + expires + "; path=/";
}
答案 0 :(得分:0)
好吧,在我的浏览器上,您的代码对两个文本区域均适用,请参见以下代码段:
jQuery(document).ready(function($){
$("#name").change(function(){
var name1 = $("textarea#name").val();
$(this).next('.namme').text("Запам'ятовано");
createCookie("namme", name1, "1");
});
$('.name').trigger('change');
$('#about').change(function(){
var about1 = $('#about').val();
$(this).next('.abbout').text("Запам'ятовано");
createCookie("abbout", about1, "1");
});
$('.about').trigger('change');
});
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else {
expires = "";
}
document.cookie = encodeURIComponent(name) + "=" +
encodeURIComponent(value) + expires + "; path=/";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
Назва:
<textarea type="text" class="form-control" class="name" id="name"></textarea>
<span class="namme"></span>
</div>
<div>
Опис товару:
<textarea type="text" class="form-control" class='about' id='about'></textarea>
<span class="abbout"></span>
</div>