繁体   English   中英

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

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

大家好,因此我试图创建的此页面包含一些选项卡,每个选项卡的名称都为月份,并且选项卡的内容为表格和提交按钮。 即时通讯面临的问题是,例如,如果即时通讯在2月选项卡上,并且我已经填写了表格,并且当我单击提交时,页面将重新加载(将数据插入数据库)并返回到第一个选项卡,即1月。 我如何阻止这种情况发生? 表示点击提交并成功插入数据后,它仍然停留在2月选项卡上。 我读到我必须使用具有onClick属性的标记,但仅此而已。 我对javascript的了解很少。

标签内容的一部分:

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

您可以编写一个自定义函数,当有人单击“提交”按钮时将执行该函数。 该函数需要返回false以防止默认行为。

对于函数本身,我将使用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; 
}

编辑:或只是按照发布的链接:D,只是看到它具有相同的解决方案

暂无
暂无

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

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