gpt4 book ai didi

javascript - 按钮文本在 JavaScript 中没有改变?

转载 作者:行者123 更新时间:2023-11-30 11:38:13 25 4
gpt4 key购买 nike

我正在尝试将按钮的文本更改为存储在变量中的值的文本。该按钮当前为空白,甚至没有使用像 .value = "test"; 这样的固定值。

HTML:

<div id="addContainer">
<textarea id="listTitleInput" class="textarea" placeholder="Add the title of your list here, then click 'Add List'." rows="10" cols="50"></textarea>
<button id="addListBtn" data-role="button">Add List</button>
</div>

<div id="listDisplayContainer">
</div>

JS:

$(document).ready(function () {

//LISTS
var listTitleInput = document.getElementById("listTitleInput");
var addListBtn = document.getElementById("addListBtn");
var listCount = localStorage.getItem("listCount");

if (listCount === null) {
noteCount = 0;
}

//ADD LISTS
function addList() {
if ($("#listTitleInput").val() == "") {
alert("Please give your list a title, then click 'Add List'.");
} else {
listCount++;
var list = $("#listTitleInput").val();
console.log("List Count: " + listCount);
console.log(list);
var display = document.createElement("button");
document.getElementById("listDisplayContainer").appendChild(display);
display.className = "ui-btn";
display.id = "list" + listCount;
$("#list" + listCount).value = list;

}
}

//Lists
addListBtn.addEventListener("click", addList);

});

最佳答案

看起来您需要将 $("#list"+ listCount).value = list; 更改为 $("#list"+ listCount).text(list);

值不是属性,val() 不适用于按钮。

关于javascript - 按钮文本在 JavaScript 中没有改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43620909/

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