用jquery和cakephp切换

时间:2011-11-17 18:58:56

标签: javascript jquery ajax cakephp

首先发布,我使用带有ajax的cakephp框架创建一个回复按钮。问题是,当用户点击回复按钮时,文本框会一直加载。如何切换,以便当用户第二次单击它时,它会关闭回复文本框。现在我可以用thiss.remove()禁用按钮;但是id非常接近文本框。我的代码如下。感谢

<script type="text/javascript">
jQuery('.reply-button').live('click', function() {
    var a = jQuery(this).data('comment_id');
    var b = jQuery(this).data('video_id');
    var c = jQuery(this).data('comment_quote');
    var d = jQuery(this).data('reply_name');
    var e = jQuery(this).data('quote_body');
    var f = jQuery(this).data('topic_name');
    var thiss = jQuery(this);
    jQuery.ajax({
        type: "POST",
        url: "<?= $this->Html->url(array('controller' => 'comments', 'action' => 'reply')); ?>",
        data: { comment_id: a, video_id: b, comment_quote: c, reply_name: d , quote_body: e , topic_name: f },
        success: function(html) {
            thiss.parent().parent().parent().append(html);
        }
    });
});

1 个答案:

答案 0 :(得分:0)

如果您的文本框具有idclass属性,我会使用它来确定它是否可见/隐藏/已加载并基于该隐藏/显示/加载它。

例如,假设文本框的ID为reply,您可以这样做:

jQuery('.reply-button').live('click', function() {

    // Check if the textbox is on the page or if we need to load it via an ajax call
    if (jQuery('#reply').length == 0)
    {
        // The textbox isn't loaded on the page so call the ajax and append it     
        var a = jQuery(this).data('comment_id');
        var b = jQuery(this).data('video_id');
        var c = jQuery(this).data('comment_quote');
        var d = jQuery(this).data('reply_name');
        var e = jQuery(this).data('quote_body');
        var f = jQuery(this).data('topic_name');
        var thiss = this;

        jQuery.ajax({
            type: "POST",
            url: "<?= $this->Html->url(array('controller' => 'comments', 'action' => 'reply')); ?>",
            data: { comment_id: a, video_id: b, comment_quote: c, reply_name: d , quote_body: e , topic_name: f },
            success: function(html) {
                thiss.parent().parent().parent().append(html);
            }
        });        
    }
    else
    {
        // The textbox is on the page so check if its visible/showing
        if (jQuery('#reply').is(':visible'))
        {
            // The textbox is visible so hide it
            jQuery('#reply').hide();
        }
        else
        {
            // The textbox is hidden so show it
            jQuery('#reply').show();
        }
    }
});

修改:我还将您的var thiss = jQuery(this);更改为var thiss = this;,因为this已经是jQuery对象,所以它不需要经过{{1把它变成一个。