我有以下简单的代码,我需要在单击按钮时触发警报。但不会触发该事件。
@{
ViewBag.Title = "Dashboard";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$("#btnclickhere").click(function (e) {
alert(1);
e.stopPropagation(); // This is the preferred method.
return false; // This should not be used unless you do not want
// any click events registering inside the div
});
</script>
<h2>Dashboard</h2>
<table>
<tbody>
<tr>
<td>
<input type="text" id="textname" name="txtname" />
</td>
<td>
<input type="text" id="textpassword" name="textpassword" />
</td>
<td>
<input type="button" id="btnclickhere" name="btnclickhere" value="ClickHere" />
</td>
</tr>
</tbody>
</table>
有人可以帮助我解决这里的问题吗?
脚本在 html 标记之前加载,因此在您要求 jQuery 注册单击处理程序时,没有任何调用 #btnclickhere。你应该把你的jquery代码包装在一个$(document(.ready(function(({/your code here/}(;事件处理程序,用于确保在查询之前完全填充 DOM。