首页 > 解决方案 > 表单内的按钮不可访问

问题描述

我正在尝试使用 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 代码之前呈现。谢谢。

标签: javascriptjqueryhtmleventsevent-handling

解决方案


你可以试试

$(document).on("click",".switchLang",function (e) {
    console.log("Clicked!")
});

推荐阅读