简体   繁体   中英

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

I need to get comment id when clicking to report button (I want to get comment-6) Now when I click 'report' it show a modal box with form.

    <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>

clicking to

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

call jquery

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

and function is

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');
}

I tried to do it but nothing. I suppose it is possible otherwise will think to change code. I hope someone will help me.

If i understand you correctly, the following should fetch the comment id for you:

$(".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
});

You can use closest to get the closest element which cotains the required class and get its id. Try this.

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

Now use $('#report-window').data('commentid') to get the current comment id inside checkReportForm method.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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