【发布时间】:2013-04-24 12:34:55
【问题描述】:
我在这样的表单中有一个按钮:
<form>
<input type="text" />
<input type="password" />
<button type="button" class="btn-login">Log in</button>
</form>
我正在尝试通过在 js 中执行此操作来触发按钮的单击事件:
$(document).ready(function () {
$('.btn-login').live("click", function () {
alert("Button clicked!");
});
});
但由于某种原因,这不起作用。
如果我删除了文本框和按钮周围的<form></form>,就会触发点击事件,但这也意味着我的页面没有正确显示,所以我想知道是否有办法触发按钮的点击事件在表单内时。
【问题讨论】:
-
不知道为什么将其包装在
<form>标记中会破坏它,但尝试使用.on('click'而不是.live -
这个表单是通过 ajax 在 DOM 中添加的,就像你使用 live 一样
-
按照
http://www.w3schools.com/tags/tag_button.aspTips and Notes Note: If you use the <button> element in an HTML form, different browsers may submit different values. Use <input> to create buttons in an HTML form.
标签: javascript jquery html forms click