繁体   English   中英

带提交按钮的Onclick无法正常工作

[英]Onclick with submit button not working properly

我想点击“提交”按钮。 我在许多教程网站上搜索了所有提示,但所有提示都无法解决问题。 当我在调用的函数中放置警报时,警报正在运行,但是page-join.php内部的任务无法运行。 当我在onclick上使用图像时效果很好。 请指导什么问题,由于表单的替换,我在Firebug控制台中没有遇到问题。 所以我没有真正的问题是什么。

//This is my function that i am calling from onclick.

  function deltpopdtl()
   {
  var ipss = '<?php echo $baseUrl ;?>/themes/gr-mist/includes/';

  $.ajax({
  url: ipss+"page-join.php?delpopdtl=<?php echo $_GET['pageid'];?>",
  success: function(data){
     }
  });}


// This is my php code(page-join.php)
     if(isset($_GET['delpopdtl'])) 
                {
       global $db;
       $getdetail = "DELETE  FROM firstloadpop WHERE rsc_id=".$_GET['delpopdtl']." and user_id= $user_id";

            mysql_query($getdetail);
             }
//This is my form and submit button
<form method="post" id="" enctype="multipart/form-data" action="#"> 

<input type="submit" id="abc" name="onladinvite" value="sendinvitation" onclick="return deltpopdtl();" />
 //i put only onlick deltpopdtl(); but the same situation, i also tired
 //onclick="return deltpopdtl();return false" but all vain

</form>

你可以这样写

function deltpopdtl()
   {
  var ipss = '<?php echo $baseUrl ;?>/themes/gr-mist/includes/';

  $.ajax({
  url: ipss+"page-join.php?delpopdtl=<?php echo $_GET['pageid'];?>",
  success: function(data){
     }
  });

   return false;
}

您可以在函数主体的末尾添加return false ,这样就不必提交表单,也可以使用firebug进行调试

事实是,当您提交表单时,代码将执行,而这样做会中断并重新加载页面。 为了防止这种情况,您可以使用event.preventDefault(); 以防止提交按钮的默认性质。

function deltpopdtl(event){
  event.preventDefault();
  var ipss = '<?php echo $baseUrl ;?>/themes/gr-mist/includes/';

  $.ajax({
     url: ipss+"page-join.php?delpopdtl=<?php echo $_GET['pageid'];?>",
     success: function(data){
     }
  });
}

//your html button
<input type="submit" id="abc" name="onladinvite" value="sendinvitation" onclick="deltpopdtl(event);" />
  1. 您必须在函数结束之前编写return false ,因为您的表单已提交并且看不到ajax的效果。

  2. 您可以将submit按钮更改为type='button'

function deltpopdtl()
{
  var ipss = '<?php echo $baseUrl ;?>/themes/gr-mist/includes/';    
  $.ajax({
         url: ipss+"page-join.php?delpopdtl=<?php echo $_GET['pageid'];?>",
         success: function(data){}
  });
  return false;  //You have to add this line because your form is submitted and you cant see your ajax effec
}

暂无
暂无

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

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