简体   繁体   English

onclick jQuery提交表单而不重新加载页面

[英]onclick jquery to submit form without reloading the page

hey guys so this page im trying to create has tabs with the names of months for each tab and the content of the tabs is a form and a submit button. 大家好,因此我试图创建的此页面包含一些选项卡,每个选项卡的名称都为月份,并且选项卡的内容为表格和提交按钮。 The problem im facing is, for example, if im on the February tab and i have filled in the form and when i click submit the page reloads(the data inserted into database) and goes back to the first tab which is January. 即时通讯面临的问题是,例如,如果即时通讯在2月选项卡上,并且我已经填写了表格,并且当我单击提交时,页面将重新加载(将数据插入数据库)并返回到第一个选项卡,即1月。 how do i stop that from happening? 我如何阻止这种情况发生? meaning after clicking on submit and data is inserted successfully it still stays on the February tab. 表示点击提交并成功插入数据后,它仍然停留在2月选项卡上。 i read that i have to use tag with onClick attribute but thats it. 我读到我必须使用具有onClick属性的标记,但仅此而已。 my knowledge of javascript is minimal. 我对javascript的了解很少。

part of the tab with content: 标签内容的一部分:

             <div class="tabContent" id="feb">
         <form method="post" action="income.php">
    <input type="hidden" name="feb" value="February">
            <center><h2>February </h2></center>
            <div>
    <div style="height:0px;">
    <div class="rmtotal">
         <h3>Your Total Income :</br> RM   <input type="text" id="febtotal"         
                   name="febtotal" size="11" readonly value="<?php if(@$fsql) {echo   
                       htmlentities(@$fprev['total']);} if(isset($_POST['febtotal']))
                         {echo htmlentities($_POST['febtotal']);}?>" ></h3>
    </div>
    <input type="submit" value="Save" class="save" name="febsave">
    <button type="submit" class="prev" name="febprev" id="button" 
             onClick="">Select from previous Month</button>
    </div>
    <table border="1" rules="groups" cellpadding="10px;" class="tableincome">
    <thead>
        <th>Monthly Salary</th>
        <th></th>
        <th>RM</th>
    </thead>
    <tr>
        <td>Basic Salary</td>
        <td></td>
        <td><center><input type="text" class="feb" placeholder="0" 
                name="febbasic" size="11" value="<?php if(@$fsql) {echo 
          htmlentities(@$fprev['basic']);} if(isset($_POST['febbasic'])){echo 
             htmlentities($_POST['febbasic']);}?>"></center></td>
    </tr>
    </table>
    </form>
      </div>
      </div>  

You can write a custom function that will be executed when someone clicks the submit button. 您可以编写一个自定义函数,当有人单击“提交”按钮时将执行该函数。 That function needs to return false to prevent the default behavior. 该函数需要返回false以防止默认行为。

For the function itself I would use Ajax to send an asynchronous request to the server that can store the data in the database. 对于函数本身,我将使用Ajax将异步请求发送到可以将数据存储在数据库中的服务器。 If you use jQuery it will be very easy to use Ajax: 如果您使用jQuery,则使用Ajax将非常容易:

function handleClick(){
   //send the data to the server
   $.post(/*...see jQuery documentation*/)

   //prevent the form from submitting and the page from reloading
   return false; 
}

EDIT: Or just follow the posted link :D, just saw it has the same solution 编辑:或只是按照发布的链接:D,只是看到它具有相同的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM