简体   繁体   English

如何从标签列表到JavaScript变量获取对象属性?

[英]How to get an object attribute from a tag list to a javascript variable?

So I have this list of people : 所以我有这个人名单:

#{list celibs}
        <li class="listCelibs">
            #{encart.moreInfoProfileSwitch
            index:_,
            owner:_,
            index:_.id,
            lang:'fr',
            mainPhoto:_.getProfileImage(),
            dateVision:_.getDateVision(),
            age:_.getAge(),
            taggedById:userSession,
            fadeToggleId:'more_info_user_' + _.id /}

            <a href="#more_info_user_${_.id}" class="ubeLightBox"><img width="263px" height="215px" src="${_.getProfileImage()}"></a>
            <p class="name">${_.nickName} <span>${_.getAge()} &{'rdv.rdvProposition.profile.age_' + _.mySex.keyName}</span></p>
            <a href="#more_info_user_${_.id}" class="ubeLightBox link1">&{'rdv.rdvProposition.profile.discover_' + sex}</a>

            <p class="askInterest">Intéressé ?</p>
            <button id="No">NON</button>
            <button id="Yes">OUI</button>
        </li>

    #{/list}

They are shown one by one and I would like to make an ajax request each time "No" or "Yes" are clicked. 它们被一个接一个地显示,每次单击“否”或“是”时,我都希望发出一个ajax请求。

$("#Yes, #No").click(function(){
    $.ajax({
    url: 'addLike',
    data: {
           id : '${_.id}'
           }
    });
}); 

The problem is that I only get the last id of the list. 问题是我只得到列表的最后一个ID。 And it never changes. 而且它永远不会改变。 I would like to have the id of the current person on the screen in order to send it to the ajax request. 我想在屏幕上显示当前人的ID,以便将其发送到Ajax请求。

Try this, change id of Yes / No button to class and put input hidden with value= 'person id'. 尝试此操作,将“是” /“否”按钮的id更改为class然后将input隐藏为value ='person id'。

#{list celibs}
        <li class="listCelibs">
            #{encart.moreInfoProfileSwitch
            index:_,
            owner:_,
            index:_.id,
            lang:'fr',
            mainPhoto:_.getProfileImage(),
            dateVision:_.getDateVision(),
            age:_.getAge(),
            taggedById:userSession,
            fadeToggleId:'more_info_user_' + _.id /}

            <a href="#more_info_user_${_.id}" class="ubeLightBox"><img width="263px" height="215px" src="${_.getProfileImage()}"></a>
            <p class="name">${_.nickName} <span>${_.getAge()} &{'rdv.rdvProposition.profile.age_' + _.mySex.keyName}</span></p>
            <a href="#more_info_user_${_.id}" class="ubeLightBox link1">&{'rdv.rdvProposition.profile.discover_' + sex}</a>

            <p class="askInterest">Intéressé ?</p>
            <button class="No">NON</button>
            <button class="Yes">OUI</button>
            // put hidden input here
            <input type="hidden" class="personId" value="${_.id}">

        </li>

    #{/list}

jQuery : use below query to call ajax on click of Yes / No button jQuery:使用以下查询在单击“是/否”按钮时调用ajax

$(".Yes, .No").click(function(){
    $.ajax({
    url: 'addLike',
    data: {
           id : $(this).next('.personId').val();
           }
    });
});

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

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