javascript - 表单内的按钮不可访问
问题描述
我正在尝试使用 jQuery 触发表单内按钮的单击事件。
$(".switchLang").on("click", function(e) {
console.log("Clicked!")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="form-inline" action="">
<button type="button" class="btn mr-3 btn-link transparent switchLang">${{index.keys.lng}}$</button>
<button type="button" class="btn mr-3 btn-outline-light">${{index.keys.login}}$</button>
<button type="button" class="btn btn-primary">${{index.keys.getStarted}}$</button>
</form>
上面的函数没有被调用!
经过研究,我尝试了许多解决方案,但没有任何效果。就像e.preventDefault();
在函数里面添加一样,还有remove.click
和replace with.on
等等,但是没有用!
我已经提到了这个问题:here。
谢谢。
解决了
我仔细检查了我的代码。html 是在 text/template 类型的脚本中编写的,并且此模板在 jquery 代码之前呈现。谢谢。
解决方案
你可以试试
$(document).on("click",".switchLang",function (e) {
console.log("Clicked!")
});
推荐阅读
- python - 在 Power BI 中编辑用作数据条目的 python 脚本
- python - 无法从父目录导入文件
- c++ - 对象中 vec3 的值
- performance - Spark Structured Streaming - WAL 性能下降
- sql-server - 当列是 XML 数据类型时的 where IN 子句
- regression - 在每个参与者进行多次试验的主题设计之间
- java - 简单 == 而不是 doubleToLongBits
- c# - 尝试登录 HttpWebRequest 和 HttpWebResponse c# WPF
- windows - 以全屏/信息亭模式启动桌面 PWA
- powershell - 在Powershell中将管道分隔文件更改为逗号分隔