简体   繁体   English

可点击 <li> -单击功能扩展到其他项目

[英]Clickable <li> - click function of the last item spreading to other items

I'm creating a list of clickable divs to show a pop up window with the information relative to the clicked item. 我正在创建可单击的div列表,以显示一个弹出窗口,其中包含与单击的项目相关的信息。

Every list item is assuming the 'on click' function of the last item of the list. 每个列表项都假定列表的最后一项具有“单击”功能。 When I remove the last item then they all assume the 'on click' function of the new last item. 当我删除最后一个项目时,它们都会承担新的最后一个项目的“单击”功能。

(ex: I click on any item on the list and a pop window appears with the info about the last item. I remove it by clicking the button 'approve'. When i click on any item now a pop window will appear with the info of the new last item) (例如:我单击列表上的任何项目,然后会出现一个弹出窗口,其中包含有关最后一项的信息。我通过单击“批准”按钮将其删除。当我单击任何项​​目时,现在将出现一个弹出窗口,其中包含该信息新的最后一项)

What I want is for it to show the info of the clicked item. 我想要的是它显示单击项的信息。

HTML HTML

<ul id='absencesToApproveList'></ul>
<div class="popupBox approveAbsenceBox">
    <h3>Absence to Approve</h3>
    <div class="absencePhoto"></div>
    <p class="noteAbsence" ></p>        
    <button id="rejectAbsence"  style="width:40%; display:block-inline; float:left;">Reject</button>
    <button id="approveAbsence" style="width:40%; display:block-inline; float:right;">Approve</button>
</div>

JS JS

  function getAbsencesToApprove_success(tx, results) {
      var len = results.rows.length;
      if(internetStatus != 'none')
        $('#absencesToApproveList').empty()

      for (var i=0; i<len; i++) {
        var absence = results.rows.item(i);         
        var absenceType = absence.type;     
        var absenceText = absence.note;         
        var absenceDateFrom = absence.absenceFrom;      
        var absenceDateTo = absence.absenceTo;
        var yyyy = absenceDateFrom.split("-")[0];                   
        var mm = absenceDateFrom.split("-")[1];         
        var dd = absenceDateFrom.split("-")[2];
        var newAbsenceBox = $('<div class="absenceBox" style="border: 2px solid black; position:relative;" ')
                .append($('<div class="absenceDateBox" style="border: 2px solid black;" ')
                .append('<span style="display: inline-block;vertical-align: middle; line-height: normal;" </span ').text(dd + "/" + mm))
                .append($('<p class="absenceText" ').text(absenceText));
        var newAbsenceBoxItem = $('<li id="absence' + absence.id + 'Item" style="margin-left:-35px;" ').append(newAbsenceBox)
        $('#absencesToApproveList').append(newAbsenceBoxItem)
        $('#absence' + absence.id + 'Item').click(function(){   
            $('.absencePhoto').text('photo' + absence.id)
            $('.noteAbsence').text(absenceText)
            $('#addAbsenceBoxOuter2').show()            
            $('.approveAbsenceBox').show()
            $('#rejectAbsence').click(function () {     
                $('#addAbsenceBoxOuter2').hide()
                $('.approveAbsenceBox').hide()
                $('.absencePhoto').empty()
                $('.noteAbsence').empty()       
            })
            $('#approveAbsence').click(function () {
                $('#addAbsenceBoxOuter2').hide()
                $('.approveAbsenceBox').hide()
                $('.absencePhoto').empty()
                $('.noteAbsence').empty()
                db.transaction( 
                        function(tx){
                            tx.executeSql("DELETE FROM absences WHERE id=" + absence.id +";");
                        });

                db.transaction(
                        function(tx) {        
                            tx.executeSql("INSERT INTO absences (id,type,absenceFrom,absenceTo,note,aproved,picture) VALUES (" 
                                    + id++                                                                                                                        
                                    + ",'"                                                                                                                  
                                    + String(absenceType)                           
                                    + "','"                         
                                    + String(absenceDateFrom)                               
                                    + "','"                                 
                                    + String(absenceDateTo)                             
                                    + "','"                                 
                                    + absenceText                               
                                    + "',"                              
                                    + "'true'"  
                                    + ","                               
                                    + "'image.jpg'"                                 
                                    + ")");
                        }, transaction_error);

                db.transaction(getAbsences, transaction_error); 

            })      
          })
      }
      $('#absencesToApproveList').show()    
  }

Demo: http://jsfiddle.net/8LdKh/3/ Thanks in advance 演示: http : //jsfiddle.net/8LdKh/3/在此先感谢

The variables absence.id & absenceText can't be used because it will contain last value in the loop. 无法使用变量missing.id和missingText,因为它将在循环中包含最后一个值。 Instead you will have to get the selected ID somehow and use it. 相反,您将必须以某种方式获取所选的ID并使用它。 Here is a simple approach. 这是一个简单的方法。

var selectedID = this.id.substring(7,8); //Will work only for one digits
//$('.absencePhoto').text('photo' + absence.id)
$('.absencePhoto').text('photo' + selectedID) 

Similarly have to get the absenceText value, 同样,必须获取missingText值,

 //adding id on creation
 .append($('<p id="absenceText' + i + '" class="absenceText">').text(absenceText));
 //using id to fetch the value
 $('.noteAbsence').text($("#absenceText" + selectedID ).text())

Fiddle here 在这里摆弄

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

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