简体   繁体   English

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

[英]Onclick with submit button not working properly

I want to put onlick for submit button. 我想点击“提交”按钮。 I googled many tutorial sites implement all the hints but all that can't solve the problem. 我在许多教程网站上搜索了所有提示,但所有提示都无法解决问题。 When i put an alert inside the called function the alert is working but the tasks which is inside of page-join.php not work. 当我在调用的函数中放置警报时,警报正在运行,但是page-join.php内部的任务无法运行。 When i put onclick with an image that's work pretty well. 当我在onclick上使用图像时效果很好。 What the problem please guide, Due to the form submtion i am not getting the problem in firebug console. 请指导什么问题,由于表单的替换,我在Firebug控制台中没有遇到问题。 So i didn't get what the real problem is. 所以我没有真正的问题是什么。

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

you can write like this 你可以这样写

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 can add return false at the end of function body so it do not submit form and also you can debug with firebug 您可以在函数主体的末尾添加return false ,这样就不必提交表单,也可以使用firebug进行调试

The thing is that when you submit the form the code execute and while doing so it's interrupted and the page reloads. 事实是,当您提交表单时,代码将执行,而这样做会中断并重新加载页面。 To prevent that you can use the event.preventDefault(); 为了防止这种情况,您可以使用event.preventDefault(); to prevent the default nature of a submit button. 以防止提交按钮的默认性质。

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. You have to write return false before function end because your form is submitted and you cant see your ajax effect. 您必须在函数结束之前编写return false ,因为您的表单已提交并且看不到ajax的效果。

  2. You can change your submit button to type='button' 您可以将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