javascript - 单击按钮添加“未定义”而不是项目
问题描述
我只想将我的项目添加到列表中。
我试过把"constant value"
而不是candidate.value
; 然后显示该值。
<div class="card-body">
<ul id="dynamic-list"></ul>
<input type="text" id="candidate" />
<button onclick="addItem()">add item</button>
<button onclick="removeItem()">remove item</button>
<script type="text/javascript">
function addItem() {
var ul = document.getElementById("dynamic-list");
var candidate = document.getElementById("candidate");
var li = document.createElement("li");
li.setAttribute('id', candidate.value);
li.appendChild(document.createTextNode(candidate.value));
ul.appendChild(li);
}
function removeItem() {
var ul = document.getElementById("dynamic-list");
var candidate = document.getElementById("candidate");
var item = document.getElementById(candidate.value);
ul.removeChild(item);
}
</script>
</div>
该项目应通过单击“添加项目”按钮添加,但它被添加为undefined
.
这是 IDE 的屏幕截图:
解决方案
推荐阅读
- javascript - 当 to 值更改时的 Item.to.getAsync() 被触发到延迟
- javascript - 带有中间图像预览的多个图像/缩略图轮播/滑块
- angular - Angular 6在http拦截器中获取请求自定义参数
- angular - ngModel 在标签输入中更新时不起作用
- soapui - 如何通过 SopaUI 在 VSTS 中通过 api 标记测试用例通过/失败时向测试用例添加注释
- database - SAP-HANA 查询执行显示语法错误。任何人都可以看看这个
- mongodb - 使用 Spring Data Mongo 从文档中找到不同的嵌入文档?
- php - 使用 PHP 在文件 txt 中搜索名称
- javascript - 当 gulp-load-plugins 提供时,`gulp-postcss is not a function` 类型错误
- dart - 在 GridView 生成器中选择项目效果