.click()jquery函数无效

时间:2013-09-05 16:42:25

标签: php jquery cakephp

我在php中写了这段代码:

public function getinfo($username){
        $this->autoRender = false;
        if($this->request->is('ajax')){
            if(!ereg('^[A-Za-z0-9_.]+$',$username)){
                echo 'username';
            }
            else{
                $user = $this->User->find('all',array('conditions'=>array('User.username'=>$username)));
                if(empty($user)){
                    echo 'Fail';
                }
                else{
                    $this->loadModel('Question');
                    $question = $this->Question->find('all',array('conditions'=>array('Question.id'=>$user[0]['User']['questionid'])));
                    echo 'Sec Question : ' . $question[0]['Question']['title'] . '<br />';
                    echo 'Answer: <input type="text" id="userAnswer" class="loginField" name="data[answer]" /> ';
                    echo '<input type="submit" id="sendAnswer" class="button" value="send" /> <br />';
                    echo '<span id="recoverErr"></span>';
                    $this->Session->write('recoverPass',$user[0]);
                }
            }
        }
        else{
            $this->redirect(array('controller'=>'message','action'=>'forbidden'));
        }
    }

我在jquery文件中写了这个:

$('#send').click(function(){
    var recover = $('#recoverUsername').val();
    $('#recErr').css('color', 'red');
    if(recover == ''){
        $('#recoverUsername').focus();
        $('#recErr').html('Enter username');
        return false;
    }
    $.ajax({
        url: $('#base').html() + '/users/getinfo/'+recover,
        type: 'POST',
        success: function(data){
            if(data.match('username')){
                $('#recErr').html('Enter correct username.');
            }
            else if(data.match('Fail')){
                $('#recErr').html("This username doesn't exist");
            }
            else{
                $('#recErr').html('');
                $('#recoverWindow').html(data);
                $('#recoverWindow').dialog('open');                    

            }
        }
    });
});
$('#sendAnswer').click(function(){
                    var answer = $('#userAnswer').val();
                    $.ajax({
                        url: $('#base').html() + '/users/getanswer/'+answer,
                        type: 'POST',
                        success: function(data){                                
                            if(data.match('answer')){
                                $('#recoverErr').html('Enter answer');
                            }
                            else if(data.match('Fail')){
                                $('#recoverErr').html('answer is false.');
                            }
                            else if(data.match('Bad')){
                                $('#recoverErr').html('fail too send mail.');
                            }
                            else{
                                $('#recoverWindow').html('');
                                $('#recoverWindow').html('Email was sent, check your spam if it is not in your inbox.');
                            }
                        }
                    });});

但是当我点击并且服务器找到用户的信息并将其放入recoverWindow时,点击功能不起作用,并且不会向操作发送答案。 请帮助我,我没有时间

3 个答案:

答案 0 :(得分:1)

您已经使用Ajax在php函数中创建恢复表单。所以你不能把$('#sendAnswer').click()放在准备好的功能中。因为您的HTML中不存在sendAnswer元素,并且您希望在php文件中创建。 所以你应该在ajax执行后为这个元素编写click函数。有了这个解释,您的JQuery代码应该改为:

$('#send').click(function(){
var recover = $('#recoverUsername').val();
$('#recErr').css('color', 'red');
if(recover == ''){
    $('#recoverUsername').focus();
    $('#recErr').html('Enter username');
    return false;
}
$.ajax({
    url: $('#base').html() + '/users/getinfo/'+recover,
    type: 'POST',
    success: function(data){
        if(data.match('username')){
            $('#recErr').html('Enter correct username.');
        }
        else if(data.match('Fail')){
            $('#recErr').html("This username doesn't exist");
        }
        else{
            $('#recErr').html('');
            $('#recoverWindow').html(data);
            $('#recoverWindow').dialog('open');
            $('#sendAnswer').click(function(){
                var answer = $('#userAnswer').val();
                $.ajax({
                    url: $('#base').html() + '/users/getanswer/'+answer,
                    type: 'POST',
                    success: function(data){                                
                        if(data.match('answer')){
                            $('#recoverErr').html('Enter answer');
                        }
                        else if(data.match('Fail')){
                            $('#recoverErr').html('answer is false.');
                        }
                        else if(data.match('Bad')){
                            $('#recoverErr').html('fail too send mail.');
                        }
                        else{
                            $('#recoverWindow').html('');
                            $('#recoverWindow').html('Email was sent, check your spam if it is not in your inbox.');
                        }
                    }
                });});


        }
    }
});});

答案 1 :(得分:0)

如果你的元素id =“sendAnswer”是通过ajax加载的,你在主页面中为它写了click事件,那么你必须使用.on()或.live()方法来执行它。

但是它们都用于不同的jQuery版本。

请将其写成以下

$(document).ready(function() {

      //if you are using jQuery version after 1.7 then use following
      $(document).on('click', '#sendAnswer', function(){
             //your logic
      });

      //if you are using jQuery version upto 1.7 then use following
      $('#sendAnswer').live('click', function(){
             //your logic
      });

});

答案 2 :(得分:0)

  

帮助我,我没有时间

这就是你没有搜索其他相关答案的原因..

无论如何像stackoverflow中的许多其他答案一样,包括我的,这里我再去..

您需要使用on

为动态添加的元素委派点击事件
$('#recoverWindow').on('click','#sendAnswer',function(){
  ....

而不是

$('#sendAnswer').click(function(){