2013-11-26 2 views
0

<ul>을 동적으로 삽입하고 .html()을 사용하여 항목을 AJAX 호출에 나열합니다. 동적으로 삽입 된 목록 항목 수를 계산하려면 <ul>.

코드 :

<div id='layout'> 
     <a href='javascript:void(0);' class='close'><img src='images/close.png' width='17' /></a> 
     <div id='buslayout'></div> 
     <div id='boarding'> 
      <select name='boarding' id='boarding'> 
       <option value=''>Boarding points</option> 
      </select> 
     </div> 
     <div class='seats'><p>Selected seats: <span id='totalseats'></span></p></div> 
     <div class='seats'><p>Total: <span id='total'></span></p></div> 
     <div class='seats'><p><input type='submit' value='Continue booking' /></p></div> 
     <div class='clear'></div> 
    </div> 

jQuery를 :

$('#buslayout').html(data);

$('ul li').each(function(){ 
    price += parseInt($(this).attr('price')); 
    console.log($(this).attr('price')); 
}); 

동적으로 생성 된 HTML.

<ul> 
    <li price='100'>Bus</li> 
    <li price='200'>Bus</li> 
    <li price='250'>Bus</li> 
    <li price='450'>Bus</li> 
</ul> 
+0

) ?? –

+0

'totalseats'는 어디에 정의합니까? – George

+0

$ ('. 착석'). 각 (function() {...? 또는 총 좌석 수를 $ totalseats = '. 좌석'); – Michael

답변

2

당신이 그것을 수행하려는 경우

$('#buslayout').html(data).find('li').length 

또는 반복하면서 다음 dinamically가 HTML을 생성

var liCount = 0; 
$('ul li').each(function(){ 
    price += parseInt($(this).attr('price')); 
    console.log($(this).attr('price')); 
    liCount ++; 
}); 
+0

$ (document) .find ('ul li'); 나를 위해 일했습니다. –

-1

전체 좌석을 jQuery 변수로 정의 했습니까?

$totalseats = jQuery('.seats'); 

totalseats.each(function() { ... 

**Edit** 
Then load the file with $.ajax() and iterate over the returned data 

$.ajax({ 
    url: 'your file', 
    success: function(data){ 
     $.each(data, function(){ 
      // your func 
     }); 
    } 
}); 
+0

네,하지만 .html에서로드 된 내용 때문에() 함수 –

+0

그는 자신의 ul에있는 li 요소의 수를 계산하려고합니다. – user1021726

관련 문제