1

在一个 php 页面中,我有一个 ajax jquery 代码,它调用我的 php 控制器来执行特定的操作,当操作返回成功时,我正在刷新特定的 div。

我的疑问是,当我的控制器向我的 jquery 函数发出响应时,我想执行其他操作,例如。如果用户未登录,那么我想发出警报,例如alert("Please login to continue");

我已经尝试了下面的代码,但它根本不起作用。请帮忙

  <script type="text/javascript">         
        $('.voteup').click(function(e)
        {  
            // prevent the default action when a nav button link is clicked
            e.preventDefault(); 
            var $this = $(this),
            id = $this.data('id') 
            e.preventDefault(); 
            // ajax query to retrieve the HTML view without refreshing the page.
            $.ajax({
                type: 'POST',
                url:"<?php echo base_url('question/ajax_vote_up/'); ?>",
                data: {id: id},  
                dataType: 'html',
                success: function (html) 
                { 
                    if(html == 'not_logged_in')//This is not working, not giving alert 
                    { 
                        alert("You need to login to vote.");
                    }
                     if(html == 'own_question')//This is not working, not giving alert 
                    { 
                        alert("You can not vote for your own question");
                    }
                    else{ //when success refresh the div
                        $('#questvotediv').html(html);}
                }
            });
            return false;
        });
    </script>

在我的控制器中

function ajax_vote_up() 
{  

  $answer_id = $this->input->post('id'); 

   if ($answer_id > 0 && $this->session->userdata('logged_in') && $this->askmodel->get_user_id_By_answer_id($answer_id)!=$this->session->userdata('userid')) 
   {  
     //Few business logic related to my requirement
       $this->load->view('users/pages/show_question_By_id/votediv', $data); 
  }
   else if($this->askmodel->get_user_id_By_answer_id($answer_id)==$this->session->userdata('userid'))
  {
       //when user try to vote for their own quetion
     echo "own_question";
  }
  else
    {//Not looged in
      echo "not_logged_in";
    }
} 
4

3 回答 3

0

余可以在修剪空格后尝试

  success: function (html) 
           { 
                html = $.trim(html);
                if(html == 'not_logged_in')//This is not working, not giving alert 
                { 
                    alert("You need to login to vote.");
                }
                if(html == 'own_question')//This is not working, not giving alert 
                { 
                    alert("You can not vote for your own question");
                }
                else{ //when success refresh the div
                        $('#questvotediv').html(html);}
                }
于 2013-04-11T10:40:40.023 回答
0
You need to store response inside body first. Then You can check it.

<script type="text/javascript">         
    $('.voteup').click(function(e)
    {  
        // prevent the default action when a nav button link is clicked
        e.preventDefault(); 
        var $this = $(this),
        id = $this.data('id') 
        e.preventDefault(); 
        // ajax query to retrieve the HTML view without refreshing the page.
        $.ajax({
            type: 'POST',
            url:"<?php echo base_url('question/ajax_vote_up/'); ?>",
            data: {id: id},  
            dataType: 'html',
            success: function (html) 
            { 
                $('#responseStore').html(html);

                if($('#responseStore').text() == 'not_logged_in')
                { 
                    alert("You need to login to vote.");
                }
                 if($('#responseStore').text() == 'own_question')
                { 
                    alert("You can not vote for your own question");
                }
                else{ //when success refresh the div
                    $('#questvotediv').html(html);}
            }
        });
        return false;
    });
</script>
<div style="display:none;" id="responseStore"></div>
于 2013-04-11T10:41:59.177 回答
0

可能是您在 echo 命令之前或之后输出的 PHP 文档中有一些空格。如果是这种情况,您的比较可能会很混乱。因此,我通常更喜欢使用 INT,因为在您的情况下,您可以将 PHP 的返回值设置为 0 表示未登录,1 表示自己的问题,然后在您的 JS 比较中使用 parseFloat() 函数和comparte整数的

于 2013-04-11T13:46:01.380 回答