【问题标题】:Not able to get functions on onClick event of button at runtime in javascript在运行时无法在 javascript 中获取按钮的 onClick 事件函数
【发布时间】:2013-12-14 18:34:39
【问题描述】:

我的 HTML 页面:

在页面的Header区域:

<script type="text/javascript">
    function AddEmployee() {
        jQuery.support.cors = true;
        var Product = {
            Name: $('#txtTitle').val(),
            Price: $('#txtPrice').val(),
            Category: $('#txtGenre').val()
        };

        $.ajax({
            url: 'http://localhost:62310/api/products',
            type: 'POST',
            data: JSON.stringify(employee),
            contentType: "application/json;charset=utf-8",
            success: function (data) {
                WriteResponse(data);
            },
            error: function (x, y, z) {
                alert(x + '\n' + y + '\n' + z);
            }
        });
    }
</script>

点击 btnAdd 按钮

<asp:Button ID="btnAdd" runat="server" OnClick="AddEmployee()" Text="Add" />

但是当我运行 asp.net 项目时,它说 aspx 页面不包含“AddEmployee()”函数的定义。

请提出您的建议 提前致谢。 苏尼尔·贾达夫

【问题讨论】:

  • 重要提示:页面上有一个按钮,其 onclick 事件已分配给 AddEmployee()。
  • 你在哪里使用产品?为什么不使用 jQuery 添加 onclick?在 ASP 中可能不仅仅是 onclick

标签: javascript jquery asp.net-web-api


【解决方案1】:
<asp:Button ID="btnAdd" runat="server" OnClientClick="AddEmployee()" Text="Add" />

使用OnClientClick 而不是OnClick

OnClientClick 用于在客户端处理事件,而OnClick 用于在服务器端处理事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 2018-08-13
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多