繁体   English   中英

通过单击Jquery上的按钮获取内容ID(最大)

[英]Get content id (greatest) by clicking button on it Jquery

我需要在点击报告按钮时获得评论ID(我想获得评论-6)现在,当我点击“报告”时,它会显示一个带有表单的模态框。

    <div class="comment-box medium-comment" id="comment-6">
        <div class="photo-box">
        <img src="img/samples/photo_1.jpg" alt="" class="photo rounded"/>
        <a href="#" title="" class="corner rounded"></a>
        </div>
        <div class="avatars">
        <a href="#" title="">
            <img src="img/samples/followers/1.jpg" alt=""/>dearskye
        </a>
        <span>commented on</span>
        <a href="#" title="">
            <img src="img/samples/followers/2.jpg" alt=""/>Antony12
        </a>
        </div>
        <div class="comment rounded">
        <div class="bg-tl"></div>
        <div class="text">Happy golden days of yore
                  Happy golden days of yore Happy golden days 
             of yore</div>
        <div class="buttons">
           <a href="#" title="" class="report">REPORT</a>
        </div>
        </div>
        <div class="cinfo">
        2 дня назад
        </div>
        <div class="both"></div>
    </div>

点击

<a href="#" title="" class="report">REPORT</a>

打电话给jquery

jQuery('a.report').bind('click', function(event) {
    showModalWindow('report-window');
});

和功能是

function checkReportForm(form)
{
var result=true;
var select=jQuery("#report-window select");
var textarea=jQuery("#report-window textarea");

if(textarea.hasClass('default'))
{
    //Save placeholder
    textarea.data('placeholder', textarea.text());
    textarea.toggleClass('default');
}
textarea.attr('class','rounded');

if(select.val()==0)
{
if(textarea.val()==''||textarea.val()==textarea.data('placeholder'))
{
    result=false;
    textarea.toggleClass("alert");
}
}
if(result)
{
closeModalWindow('report-window');
}

我试着这样做,但没有。 我想有可能否则会考虑改变代码。 我希望有人会帮助我。

如果我理解正确,以下内容应该为您获取评论ID:

$(".report").click(function() {
    var $box = $(this).parents(".comment-box");
    var commentId = $box.attr("id").replace("comment-", "");

    // commentId contains 6
    // call showModalBox and do whatever you want
});

你可以使用closest来获得最接近所需类的元素并获得它的id。 尝试这个。

jQuery('a.report').click(function() {
    var $commentBox = $(this).closest(".comment-box");
    var id = $commentBox.attr('id').replace('comment-', '');
    alert(id);//It will alert the comment id

    //Store the comment id in report window
    $('#report-window').data('commentid', id);
});

现在使用$('#report-window').data('commentid')来获取checkReportForm方法中的当前注释id。

this上下文是您的报告链接时:

var id = Number(this.parentNode.parentNode.id.substring(8));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM