按钮导致页面重新加载 [英] button causing page to reload

查看:70
本文介绍了按钮导致页面重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的页面上使用html和jquery。

I am using html and jquery on my page.

在我的html中,我有一个按钮,当点击它时会激活一个功能。

In my html i have one button which when clicked will fire one function.

当页面加载时,我在文档就绪中调用main函数。这是我的代码

When page loads i call main function in document ready. Here is my code

<script>
      $(document).ready(function () {
        main();

      });
      function main(){
             //some code goes here
      }

      function search(){
           //some logic
      }
</script>

<div>
  <button id="btnSearch" onclick="search()" >Search</button>
</div>

但是当我点击按钮然后进入 main 函数并在其中执行代码。为什么?它应该只调用函数搜索而不是其他任何东西。我做错了什么?

But when i click on button then it goes inside main function and executes code inside it. Why? It should only call function search and nothing else. What am i doing wrong?

推荐答案

默认情况下 BUTTONS 属于类型 SUBMIT ,试试这个

by default BUTTONS are of type SUBMIT, try this instead

<button type="button" id="btnSearch" onclick="search()" >Search</button>

这篇关于按钮导致页面重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆