Php .click()jquery函数不工作

Php .click()jquery函数不工作,php,jquery,cakephp,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

我用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'));
        }
    }
但是,当我单击时,服务器找到了用户的信息并将其放在recoverWindow中,单击功能不起作用,也不会发送操作的答案。 请帮帮我,我没有时间

如果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
      });

});
如果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
      });

});
帮帮我,我没有时间

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

不管怎样,就像stackoverflow中的许多其他答案一样,包括我的,我又来了

您需要使用on

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

$('#sendAnswer').click(function(){
帮帮我,我没有时间

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

不管怎样,就像stackoverflow中的许多其他答案一样,包括我的,我又来了

您需要使用on

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

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

您已经使用Ajax在php函数中创建了恢复表单。因此,您不能将
$('#sendAnswer')放入。请在ready函数中单击()。因为sendAnswer元素在HTML中不存在,您希望在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.');
                        }
                    }
                });});


        }
    }
});});

您已经使用Ajax在php函数中创建了恢复表单。因此,您不能将
$('#sendAnswer')放入。请在ready函数中单击()。因为sendAnswer元素在HTML中不存在,您希望在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.');
                        }
                    }
                });});


        }
    }
});});

发生了什么事?是否调用了单击处理程序?是否进行了AJAX调用?它是否正在发送您希望它发送的数据?服务器的响应是什么?JavaScript控制台上是否有错误?PHP日志中是否有错误?您需要进行一些调试。仅仅发布您的所有代码供我们为您修复并不是一个真正的问题。可能代码在单击事件中丢失了。。。尝试绑定此
$('#sendAnswer')。绑定('click',function(){…})需要更多信息来帮助您。您是否检查了控制台以查看ajax调用是否成功发送?我已经对其进行了测试,单击功能无法完全工作。发生了什么?是否调用了单击处理程序?是否进行了AJAX调用?它是否正在发送您希望它发送的数据?服务器的响应是什么?JavaScript控制台上是否有错误?PHP日志中是否有错误?您需要进行一些调试。仅仅发布您的所有代码供我们为您修复并不是一个真正的问题。可能代码在单击事件中丢失了。。。尝试绑定此
$('#sendAnswer')。绑定('click',function(){…})需要更多信息来帮助您。你检查控制台看ajax调用是否成功发送了吗?我已经测试过了,点击功能没有完全工作。是的,你是对的,非常感谢,我的朋友,现在它工作得很好。是的,你是对的,非常感谢,我的朋友,现在它工作得很好。