2011-08-17 2 views
1

저는 JQuery와 Javascript를 처음 접했고 JQueryUI ProgressBar를 사용하고 있습니다. 누군가 버튼을 클릭 할 때마다 진행률 막대가 움직이며 채워집니다. 버튼이 이벤트를 다시 트리거하지 못하도록 이미 눌려진 것을 추적하는 방법이 있습니까?버튼 이벤트가 Javascript/JQuery에서 한 번만 트리거되는지 확인하는 방법은 무엇입니까?

현재 HTML :

<ul> 
    <li><a href="left/section1.html" target="leftcontainer" onClick="updateBar();window.presentation.location.href='right/section1/page1.html';;return true;">overview</a></li> 
    <li><a href="left/section2.html" target="leftcontainer" onClick="updateBar();window.presentation.location.href='right/section1/page1.html';;return true;">key features</a></li> 
    <li><a href="#" onClick="updateBar()">customers</a></li> 
    <li><a href="#" onClick="updateBar()">accessories</a></li> 
    <!--<li><a href="#">nav5</a></li> 
    <li><a href="#">nav6</a></li> --> 
    </ul> 

현재 JQuery와/자바 스크립트 :

var percentage = 0;              

function updateBar() 
{ 
    percentage += 25;                
    $('.ui-progressbar-value').stop().animate({ width: percentage+"%" }, 500)  
    $('.middle').progressbar('option','value', percentage);       

    if(percentage > 100)                
    { 
     $('ui-progressbar-value').stop().animate({ width: "100%"}, 500);    
    } 
} 

$(function() { 
    $('.middle').progressbar({           
     value: percentage,            
     complete: function() { alert('The progress bar is full'); }  
    }); 

답변

4

나는 자바 스크립트의 클릭 이벤트를 바인딩과 jQuery를 .one() 방법을 사용합니다.

documentation

는 기본적으로 하나의 방법은 그러한 클릭 이벤트로, 한 번 트리거 할 수있는 이벤트를 바인딩 할 수 있습니다.

Live Demo

var percentage = 0;              

$('ul li a').one('click', updateBar); 

function updateBar() 
{ 
    percentage += 25;                
    $('#test').text(percentage); 
} 
+0

. JQuery 문서에서 좀 더 자세히 살펴볼 것이다. 고마워요! : D – lislis

+0

페이지가 새로 고침/다시로드 된 후 사용자는 다시 한 번 기능을 호출 할 수 있습니다. 이것을 제한 할 수있는 방법이 있습니까? 이 함수는 iframe 페이지에서 호출되고 사용자가 링크를 클릭하면 한 번 호출을 무효화하여 해당 페이지가 새로 고쳐지기 때문에 묻습니다. 어떤 아이디어? – lislis

+0

페이지가 다시로드되면 JS가 다시로드됩니다. page.html? perc = 25와 같은 매개 변수를 사용하여 페이지를 다시로드 할 수 있으므로 다시로드 할 때 이미 비율이 25 %임을 알 수 있습니다. 또는 페이지 매개 변수를 통해 클릭 된 요소를 전달하십시오. 그런 다음 페이지로드시 URL 매개 변수를 구문 분석하고 링크를 비활성화해야합니다. 그게 전부 전체 이슈 전부. – Loktar

0

HTML (클래스를 추가) : 간단한 매력, 같은과 지점에 근무

<li><a href="#" class='clickable'>customers</a></li> 
<li><a href="#" class='clickable'>accessories</a></li> 

JS

var percentage = 0;              

$(function() { 
    $('.clickable').click(function updateBar(){ 
     $(this).unbind('click'); //dont allow click again 
     percentage += 25;                
     $('.ui-progressbar-value').stop().animate({ width: percentage+"%" }, 500);  
     $('.middle').progressbar('option','value', percentage);       

     if(percentage > 100)                
     { 
      $('ui-progressbar-value').stop().animate({ width: "100%"}, 500);    
     } 
    }); 

    $('.middle').progressbar({           
     value: percentage,            
     complete: function() { alert('The progress bar is full'); }  
    }); 
}); 
+0

실제로 버튼을 다시 클릭하는 것을 제한한다는 점을 제외하고는 효과가있었습니다. 그러나 나는 그것이 가지고있는 일반화가 모든 요소에 적용될 수 있기를 좋아했다. – lislis

관련 문제