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

时间:2013-12-05 18:20:13

标签: javascript php jquery

我是一个noobie,经过数小时的搜索和反复试验后仍然坚持这个问题。任何帮助将不胜感激,因为我完全没有想法。谢谢。

我有一个jquery评论提交表单,在您发布时显示评论。代码工作正常,直到我将'if语句'添加到'include'php文件中,这是我在回显的现有注释中删除按钮所需的。当我使用if语句时,按钮不再有效,触发jquery实时发布到页面上。

这是包含php脚本中的有问题的if语句:

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

这是包含脚本(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');

});

});

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。 if语句的loggedin()部分阻止了jquery正常工作。我把它拿出来就可以了。

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