2011-12-20 6 views
0

다음 jQuery 템플릿이 있는데 특정 클래스를 적용 할 때 반복 횟수를 추적해야합니다. 내가 생각할 수있는 모든 표준 자바 스크립트 변형을 시도했다.jQuery 템플릿 루프 카운터

$ i를 반복하고 템플릿에서 $ i를 어떻게 참조합니까? 당신이 필요로 수행 할 수있는 템플릿 내에서 외부 함수를 호출

<script type="text/html"id="sectionTemplate">       <span data-bind="css: { selected: $data == viewModel.selectedSection() }, click: function(i) { viewModel.selectSection($data) }"> 
${i++} 
<input id="radio" class="ui-helper-hidden-accessible" type="radio" name="radio"> 
<label class="class${i}" for="radio${i}" aria-pressed="false" role="button" aria-disabled="false"> 
<span class="ui-button-text">${$data}</span></label>          
</span> 

+0

jQuery를 템플릿, 코드 내부의 논리를 추가 지원하지 않습니다 당신이 필요로하는 경우, 즉, 같은 다른 템플릿 엔진을 시도' doT' 또는'blueimp' https://github.com/blueimp/JavaScript-Templates –

답변

2

.

<script type="text/javascript"> 
    var i = 0; 
    function count() 
    { 
     return i++; 
    } 
</script> 

그리고는 당신은 템플릿에서 다음과 같이 호출 :

<script id="sectionTemplate" type="text/x-jQuery-tmpl"> 
    ${count()} 
</script>