if语句禁用了我的jquery函数/按钮

if statement disabling my jquery function/button

本文关键字:jquery 函数 按钮 我的 语句 if      更新时间:2024-03-19

我是一个新手,经过数小时的搜索和试错,我仍然坚持这个问题。任何帮助都将不胜感激,因为我完全没有主意了。非常感谢。

我有一个jquery评论提交表单,当你发布时,它会实时显示评论。在我将"if语句"添加到"include"php文件之前,代码一直运行良好,这是我在现有注释中的删除按钮所需的。当我使用if语句时,按钮不再工作,触发jquery实时发布到页面上。

这是includephp脚本中有问题的if语句:

if(loggedin() && $d['user_id'] == $_SESSION['user_id']){ $deletebutton = '<a href="#" id="'.$d['id'].'" class="delete">x</a>';}; 

这是include脚本(comment.class.php)中有问题的if语句所在的部分:

class Comment

private $data = array();
public function __construct($row)
{
    /*
    /   The constructor
    */
    $this->data = $row;
}
public function markup()
{
    /*
    /   This method outputs the XHTML markup of the comment
    */
    // Setting up an alias, so we don't have to write $this->data every time:
    $d = &$this->data;
    //$link_open = '';
    //$link_close = '';
    //if($d['url']){
        // If the person has entered a URL when adding a comment,
        // define opening and closing hyperlink tags
        //$link_open = '<a href="'.$d['url'].'">';
        //$link_close =  '</a>';
    //}
    // Converting the time to a UNIX timestamp:
    $d['dt'] = strtotime($d['dt']);
    // Needed for the default gravatar image:
    // original code for avatar:
    //'.$link_open.'
    //<img src="http://www.gravatar.com/avatar/'.md5($d['email']).'?size=50&amp;default='.urlencode($url).'" />
    //'.$link_close.'
    //$url = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif';

    if(loggedin() && $d['user_id'] == $_SESSION['user_id']){ $deletebutton = '<a href="#" id="'.$d['id'].'" class="delete">x</a>';}; 

    return '
        <div id="comment">
            <div class="avatar">
                <a href="http://www.mywebsite.com/users/view.php?pid='.$d['user_id'].'" alt"User Link" title="User Link"><img src="http://www.mywebsite.com/users/avatar/'.$d['user_id'].'.jpg" /></a>
            </div>
            <div class="name"><a href="http://www.mywebsite.com/users/view.php?pid='.$d['user_id'].'" alt"User Link" title="User Link">'.$d['name'].'</a></div>
            <div class="date" title="Added at '.date('H:i 'o'n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
            <p class="commentText">'.$d['body'].'</p>
            '.$deletebutton.'
        </div>
    ';
}

这是comment.class.php文件的主页表单和带有include语句的回复注释代码:

<?php if(loggedin()){echo '
            <div id="addCommentContainer">
            <form id="addCommentForm" method="post" action="">
                <div>
                    <img src="http://www.mywebsite.com/images/avatar.jpg" class="avatar" />
                    <p class="commentAdd">Add your comment or review:</p>
                    <!-- <input type="text" name="link" id="link"  /> -->
                    <textarea name="body" id="body"  type="message" ></textarea>
                    <input type="image" id="submit" value="" src="http://www.mywebsite.com/images/commentsubmit.png" class="commentSubmit" />
                    <input type="hidden" name="name" id="name" value="'.$_SESSION['first'].'"/>
                    <input type="hidden" name="user_id" id="user_id" value="'.$_SESSION['user_id'].'" />
                </div>
            </form>
        </div>';} ?>    

        <?php
        /*
        /   Output the comments one by one:
        */
        foreach($comments as $c){
            echo $c->markup();
        }
        ?>

这是我为函数编写的JS脚本:

 $(document).ready(function(){
/* The following code is executed once the DOM is loaded */
/* This flag will prevent multiple comment submits: */
var working = false;
/* Listening for the submit event of the form: */
$('#addCommentForm').submit(function(e){
    e.preventDefault();
    if(working) return false;
    working = true;
    $('#submit').val('Working..');
    $('span.error').remove();
    /* Sending the form fileds to submit.php: */
    $.post('submit.php',$(this).serialize(),function(msg){
        working = false;
        $('#submit').val('Submit');
        if(msg.status){
            /* 
            /   If the insert was successful, add the comment
            /   below the last one on the page with a slideDown effect
            /*/
            $(msg.html).hide().insertBefore('#addCommentContainer').slideDown();
            $('#body').val('');
        }
        else {
            /*
            /   If there were errors, loop through the
            /   msg.errors object and display them on the page 
            /*/
            $.each(msg.errors,function(k,v){
                $('label[for='+k+']').append('<span class="error">'+v+'</span>');
            });
        }
    },'json');
});

});

我解决了这个问题。if语句的loggedin()部分阻止了jquery正常工作。我刚把它拿出来就行了。

if($d['user_id'] == $_SESSION['user_id']){ return 'content here';}