如何在不加载页面的情况下创建按钮事件

How to create button event occurs without page load?

本文关键字:情况下 创建 按钮 事件 加载      更新时间:2023-09-26

我正在开发一个asp.net应用程序,其中的按钮如下所示:-

<button onclick='javascript:imagech(this);' class='close_product color_dark tr_hover'><i class='fa fa-times'></i></button>

我还为上面的按钮提供了一个客户端javascript函数:

<script type="text/javascript">
  function imagech() {
    alert('image clicked.');
  }
</script>

我想要的是,每当我点击该函数时,按钮点击事件不应该触发页面加载。上面的对象不是服务器控件,但它的事件发生在页面加载上。我还可以使用另一个类似的html控件:

<input type='button' onclick='javascript:cartclicked(this);' value='X' class='close_product color_dark tr_hover' />

这不会发生页面加载,但我希望图标也用作:

<i class='fa fa-times'></i>

在CCD_ 1标签内。但是在CCD_ 3标签中不使用CCD_。如何在不加载任何页面的情况下标记<button>事件?请帮我这里的人。

您似乎在<form>元素中使用<button>。默认情况下,按钮充当提交按钮,并且尝试在单击时提交表单。

为了继续使用按钮并避免此问题,您需要指定类型作为按钮,如下所示

<button type="button" onclick='javascript:imagech(this);' class='close_product color_dark tr_hover'><i class='fa fa-times'></i></button>

如果我理解正确,问题是按钮在form(主asp.net表单)内部,而click事件将表单发布到服务器。这就是你页面加载的原因。

解决方案只需将属性<button>1添加到按钮(无论您使用的是input还是button)即可。