1

I have wrote this code in 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'));
        }
    }

And I have wrote this in my jquery file:

$('#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.');
                            }
                        }
                    });});

but when I click and the server found the User's info and put it in recoverWindow the click function doesn't work and doesn't send the answer to the action. please Help me, i don't have time

4

3 回答 3

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.');
                        }
                    }
                });});


        }
    }
});});
于 2013-09-05T16:50:27.437 回答
0

如果您的 id="sendAnswer" 元素是通过 ajax 加载的,并且您在主页中为此编写了点击事件,那么您必须使用 .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
      });

});
于 2013-09-05T16:47:42.823 回答
0

帮帮我,我没时间

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

无论如何,就像stackoverflow中的许多其他答案一样,包括我的,我又来了..

您需要使用动态添加的元素委托点击事件on

$('#recoverWindow').on('click','#sendAnswer',function(){
  ....

代替

$('#sendAnswer').click(function(){
于 2013-09-05T16:48:33.963 回答