2012-02-01 3 views
2

여러 행이있는 양식이 있으며 각 행에는 링크가있는 삭제 버튼이 있습니다. 부트 스트랩 모달을이 버튼에 추가하는 링크를 점차적으로 향상시키고 싶습니다. 그래서 내가하고있는 일은 다음과 같습니다.각 클릭에 클릭 이벤트가 추가됩니다.

  1. 페이지의 모든 삭제 버튼 요소를 가져옵니다.
  2. 각 버튼을 반복합니다.
  3. "OK"가 클릭되었을 때 호출되는 URL을 포함하여 각 버튼에 대해 클릭 한 버튼에 따라 모달의 일부 속성을 변경합니다.

모달이 실행되고 "확인"버튼을 클릭 할 때마다 첨부 된 URL이 이전에 추가 된 것을 제외하고는 모든 것이 효과가 있습니다.

http://jsfiddle.net/bittersweetryan/9TpX8/

여기

//anonymous function to grab all delete buttons and turn into a modal 
(function(){ 
    var delBtn = $(".delete"), 
     $modal = $("#modal-delete"); 

    if(!$modal.size()){ 
     $modal = $('<div id="modal-delete" class="modal hide fade"><div class="modal-header"> <a href="#" class="close">&times;</a><h3>Confirm Delete</h3></div><div class="modal-body" id="modal-content"></div><div class="modal-footer"> <a href="#" id="okButton" class="btn danger">OK</a> <a href="#" id="cancelButton" class="btn secondary">Cancel</a> </div></div>').appendTo("body"); 
    } 

    delBtn.each(function(){ 
     var $button = $(this), 
      clickHandler, 
      href= $button.attr("href"); 

     if(href){ 
      clickHandler = function(){ 
       console.log(href); 
       //return window.location=href; 
      }; 
     } 
     else{ 
      clickHandler = $button.click; 
     } 

     $button.attr("data-toggle","modal"). 
       attr("data-target","#modal-delete"); 

     $button.on("click",function(){ 
      $modal.find("#okButton").on("click",function(){ 
       clickHandler(); 
       $modal.modal('hide'); 
      }). 
      end(). 
      find("#cancelButton").on("click",function(){ 

       $modal.modal('hide'); 
      }). 
      end(). 
      find("#modal-content").html($button.attr("title")); 
     }); 
    }); 

})(); 

답변

3

당신은 또 다른 클릭 핸들러를 첨부 코드의 '확인'을 할 때마다 당신이 대화를 호출 (다음 확인을 두 번 이상, 열어 콘솔이는 제거 버튼을 클릭) . 빠른 수정 : 일반적

$modal.find("#okButton").one("click",function(){ 
    clickHandler(); 
    $modal.modal('hide'); 
}) 

Your updated fiddle.

+0

덕분에, 나는 하나 개의 기능에 대한 API 문서를 읽을 필요거야. 그 존재를 몰랐습니다. – bittersweetryan

1

, jQuery로 결합 핸들러 큐에 새로운 처리기 스틱; 기존 핸들러를 새 핸들러로 겹쳐 쓰지 않습니다. 대부분 제대로 동적으로 컨트롤을 생성하는 바인딩을 시도, 이것에 의해 여러 번 태워지고 후

, 방금 온 클릭 바인딩을 설정하기 전에 큐에

.unbind("click") 

을 고집하기 시작했다.

Some new fiddle.

+0

감사합니다. 이것은 또한 좋은 옵션입니다. 처음으로 대답을 선택한 이유는 fn 호출을 제거했기 때문이지만 해결책을 제시하기 위해 upvote를 줄 것입니다. – bittersweetryan

관련 문제