javascript - HTML 按钮不显示弹出窗口
问题描述
我有一个 HTML 页面,它从从 python 代码获取的列表中创建按钮。
list = ['a','b','c','d']
它将构建按钮名称 abcd,4 个按钮。
我添加了一个脚本myFunction
,单击时会弹出一个弹出窗口,以放置一些我发送到我的 python 代码的明文。这一切都适用于列表中的所有按钮,除了最后一个按钮,我不知道为什么。
只有在页面中创建的最后一个按钮不会显示弹出窗口,并决定用户在明文上输入了空响应。
代码:
<form target="_blank" method="get" action="connect" style="display: inline;">
{% if thing['x'] %}
<button class="button button" onclick="myFunction(form);openNav()"> {{ thing['letters'] }} </button>
<input type="hidden" name="reason" id="myField" value=""/>
{% endif %}
</form>
function myFunction(frm) {
var txt;
console.log(frm.reason.value); // 1st option
console.log(event.srcElement.parentElement.nextElementSibling.outerHTML) // 2nd option
var reason = prompt("Please enter reason:");
if (reason == null || reason == "") {
txt = "CANCEL";
} else {
txt = reason;
}
frm.reason.value = txt; // 1st option
console.log(event.srcElement.parentElement.nextElementSibling.value == txt); // 2nd option
return txt
}
我在 F12 中看到的错误:
Authentication:271 Uncaught TypeError: Cannot read property 'outerHTML' of null
at myFunction (Authentication:271)
at HTMLButtonElement.onclick (Authentication:230)
我看到它在这条线上给出了红色 X:
console.log(event.srcElement.parentElement.nextElementSibling.outerHTML) // 2nd option
解决方案
解决方案是删除控制台日志。这就是最后一个按钮的所有内容。
console.log(event.srcElement.parentElement.nextElementSibling.outerHTML) // 2nd option
因为它得到了 NULL 响应。
推荐阅读
- cannon.js - Cannon.js 铰链约束 - 电机:如何仅旋转一个主体
- javascript - 来自 LocalStorage 的表构建未更新
- ios - 在 iOS 上获取关于不成功配对的 BLE 配对对话框
- eclipse - SWT eclipse,删除一个用于绘制表格或树形表的 PainItem 侦听器
- c# - ASP.Net Web Forms中如何限制一天保存的记录数?
- python - 将数据从 Django 视图传递到模板
- reactjs - 没有帐单地址的 Paypal 智能按钮?
- python - 为什么我在使用 beautifulsoup 和 request 登录后无法抓取下一页?
- kotlin - @Inject 不适用于 Quarkus + Vertx + Kotlin
- typescript - 如何在 TypeScript 中创建预定义的可接受的数组值?