jQuery 实现输入框内容保存、解除和提示功能
<input id='edit_openaikey' placeholder=''>
<button id='saveBtn'>保存</button>
<button id='editBtn'>解除</button>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js'></script>
<script>
$(document).ready(function() {
// 页面加载时从localStorage中读取数据
var openaikey = localStorage.getItem('openaikey');
if (openaikey) {
$('#edit_openaikey').val(openaikey);
}
// 点击保存按钮时保存数据到localStorage
$('#saveBtn').click(function() {
var openaikey = $('#edit_openaikey').val();
localStorage.setItem('openaikey', openaikey);
alert('保存完成!');
});
// 点击解除按钮时清空数据
$('#editBtn').click(function() {
$('#edit_openaikey').val('');
localStorage.removeItem('openaikey');
});
});
</script>
原文地址: https://www.cveoy.top/t/topic/jqiI 著作权归作者所有。请勿转载和采集!