gpt4 book ai didi

javascript - localStorage 和 jQuery 问题

转载 作者:行者123 更新时间:2023-12-02 14:26:05 24 4
gpt4 key购买 nike

下面的脚本显示表单元素(文本区域)中剩余(允许)的字符。我想使用本地存储来保留在文本区域中输入的文本,直到我提交表单。

我正在尝试使用 localStorage,以便当我返回页面时 - 剩余的字符 (var text_remaining) 不会显示完整的 2000 个字符,而应该只显示剩余的字符...(var text_remaining) ???

<script>

$(document).ready(function() {
var char_th = 2000;
var text_max = 2000;

$('#textarea_feedback').html(char_th + ' characters remaining');
//$('#textarea_feedback').html(text_max + ' characters remaining');

$('#comments').keyup(function() {
var text_length = $('#comments').val().length;
var text_remaining = text_max - text_length;



$('#textarea_feedback').html(text_remaining + ' characters remaining');
});
});

</script>

这就是我最终使用的已解决 - 有效

 <script>

$(document).ready(function() {
var text_max = 2000;

$('#comments').keyup(function() {
var text_length = $('#comments').val().length;

var text_remaining = text_max - text_length;
localStorage.setItem("text", $('#comments').val());

$('#textarea_feedback').html(text_remaining + ' characters remaining');
});


$('#textarea_feedback').html(text_max + ' characters remaining');
var text = localStorage.getItem("text");

$('#comments').val(text);
$('#comments').keyup();

});

</script>

最佳答案

试试这个:

$(document).ready(function() {
var char_th = 2000;
var text_max = 2000;
$('#comments').keyup(function() {
var text_length = $('#comments').val().length;

var text_remaining = text_max - text_length;
localStorage.setItem("text", $('#comments').val());

$('#textarea_feedback').html(text_remaining + ' characters remaining');
});
$('#textarea_feedback').html(char_th + ' characters remaining');
var text = localStorage.getItem("text");

$('#comments').val(text);
$('#comments').keyup();


});

关于javascript - localStorage 和 jQuery 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38228503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com