1

I have a enable and disable button. Each buttons returns a "success" message in the console when clicked. The enable button works in a single click, however, the disable button needs to be clicked twice before it prints the "success" message in the console in my developer tools. What is the problem with this? Can somebody helps me fixing this one? Thanks a lot. Here is my code:

 <button  class="btn_mode"  value="1">Enable</button>
 <button  class="btn_mode"  value="0">Disable</button>


  <script type="text/javascript">

$(".btn_mode").click(function(){

  var mode_val = $(this).val();
  var postdata={'mode':mode_val};

  $.ajax({

    type:"POST",
    url:"<?php echo base_url();?>superadmin/change_mode",
    dataType:'json',
    data:postdata,
    success: function(data){

         if(data.notify=="Success")
            {
             console.log(data.notify);
             location.reload();
            }
            else{
              console.log(data.notify);

            }

       }
  });

});



</script>

my controller function

function change_mode(){

  $mode = $this->input->post('mode');
  $query = $this->course_booking_model->change_mode($mode);

  if($query){
    $notification = "Success";
   }
  else{
   $notification = "Failed";
   }

   echo json_encode(array('notify'=>$notification));

   }

model

function change_mode($mode){

  $id = 1;

   $data = array('mode' => $mode);

   $this->db->where('id',$id);
   $this->db->update('maintenance_mode',$data);

   return true;
  }

Output when adding the console.log('Posting mode_val: ' + mode_val); in the javascript Clicking the enable button output in the console

enter image description here Output in the console when clicking the disable button

enter image description here

4

3 回答 3

1

由于您的事件连接看起来不错,我怀疑服务器端出了点问题,这意味着您的成功处理程序没有触发。尝试在您的 ajax 调用之前记录,以查看 click 事件是否触发:

$(".btn_mode").click(function(){

  var mode_val = $(this).val();
  var postdata={'mode':mode_val};

  console.log('Posting mode_val: ' + mode_val);

  $.ajax({

    type:"POST",
    url:"<?php echo base_url();?>superadmin/change_mode",
    dataType:'json',
    data:postdata,
    success: function(data){

         if(data.notify=="Success")
            {
             console.log(data.notify);
             location.reload();
            }
            else{
              console.log(data.notify);

            }

       }
  });

});

如果在您单击按钮时上面记录了预期值,则意味着您的 php 脚本在您第一次使用mode_val = 0.

您可以尝试捕获可能引发的任何异常,如下所示:

function change_mode(){
  try{
      $mode = $this->input->post('mode');
      $query = $this->course_booking_model->change_mode($mode);

      if($query){
        $notification = "Success";
       }
      else{
       $notification = "Failed";
       }
  }
  catch(Exception $e){
    $notification = $e->getMessage();
  }
   echo json_encode(array('notify'=>$notification));

}

编辑:您上传的图像表明服务器端代码存在问题,并且您的按钮单击正常。如果你更新你的 php 来捕捉错误并返回它,你应该能够得到关于哪里出了问题的提示。要查看呼叫完成,您可以实现“完成”和/或“错误”回调:

  $.ajax({

    type:"POST",
    url:"<?php echo base_url();?>superadmin/change_mode",
    dataType:'json',
    data:postdata,
    success: function(data){

         if(data.notify=="Success")
            {
             console.log(data.notify);
             location.reload();
            }
            else{
              console.log(data.notify);

            }

       },
    error: function(jqXhr, txtStatus, errThrown){
         console.log('Call failed with status:' + txtStatus);

         if(errThrown)
             console.log('and error:' + errThrown);
       },
    complete: function(jqXhr, txtStatus){   
         console.log('Call completed with status:' + txtStatus);
       }
  });

});
于 2013-10-23T14:23:41.133 回答
0

尝试更改您的模型以返回更新的真实结果。喜欢:

return $this->db->update('maintenance_mode',$data);

取决于更新后 db 包装器返回的内容。

于 2013-10-23T14:49:14.550 回答
0

嗯,也许是这样的

$(".btn_mode[value='1']").on("click", function(){ do something });
$(".btn_mode[value='0']").on("dblclick", function(){ do something });
于 2013-10-23T13:36:54.373 回答