【问题标题】:MVC - view click event is not fired in jqueryMVC - 在 jquery 中未触发视图单击事件
【发布时间】:2017-03-27 15:48:06
【问题描述】:

我有下面的简单代码,我需要在单击按钮时触发警报。但是没有触发事件。

@{
    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>

有人可以帮我解决这里的问题吗?

【问题讨论】:

    标签: jquery model-view-controller view


    【解决方案1】:

    脚本在 html 标记之前加载,因此在您要求 jQuery 注册点击处理程序时没有任何名为 #btnclickhere 的内容。您应该将您的 jquery 代码包装在 $(document).ready(function(){/您的代码在这里/});事件处理程序,以确保在查询之前完全填充 DOM。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 2018-07-28
      • 2023-03-03
      • 2012-02-14
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多