2016-11-28 12 views
3

나는 Foundation6 공개를 사용하여 팝업 폼을 코딩합니다. 내가 추적 한 JavaScript 문제에 직면 해 있습니다. 아마도 jQuery.js/Foundation.js 이벤트 바인딩 또는 처리와 관련이 있습니다. Foundation6 공개 팝업 폼 필드 원하지 않는 포커스 손실 (jQuery 이벤트에서)

screenshot from codepen

내가 텍스트 필드 (또는 다른 필드)의 필드가 포커스를 취득한 후 창 신속하게 숨기고 다시 표시되고 필드가 포커스를 잃은 공개 창을 표시 클릭

. 공개 창은 화면의 위치도 변경합니다. 텍스트 필드에 액세스하는 유일한 방법은 탭 키를 사용하는 것입니다.

index.html을 :

<!DOCTYPE html> 
<html> 
    <head> 
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.2.4/foundation.css"/>  
     <script src="https://code.jquery.com/jquery-3.1.1.js"></script> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.2.4/foundation.js"></script> 
     <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
     <script src="app.js"></script> 
    </head> 
    <body> 
     <section class="container clearfix"> 
      <h3>Click button to open modal and test focus lost problem:</h3> 
      <p><a href="modalform.html" class="button" data-open="newFormModal" data-showloading="1">Test focus modal form</a></p> 
     </section> 
     <div class="reveal" id="newFormModal" data-reveal data-close-on-click><section class="modalContent"></section><button class="close-button" data-close aria-label="Close reveal" type="button"><i class="fi-x"></i></button></div> 
    </body> 
</html> 

modalform.html

<h3 class="modalheader">PopupForm</h3> 
<form method="post" accept-charset="utf-8" class="ajaxModal" data-showloading="1" data-open="newFormModal" action="/test/modalform3"> 
    <div style="display:none;"><input type="hidden" name="_method" value="POST" /></div> 
    <div class="input text"><label for="ga-20x28">Product 1 qty:</label><input type="text" name="ProductQuantities[726][value]" id="ga-20x28" value="0" /></div> <input type="hidden" name="ProductQuantities[726][name]" value="GA 20x28" /> 
    <div class="input text"><label for="ga-30x40">Product 2 qty:</label><input type="text" name="ProductQuantities[727][value]" id="ga-30x40" value="0" /></div> <input type="hidden" name="ProductQuantities[727][name]" value="GA 30x40" /> 
    <div class="input text"><label for="ga-50x70">Product 3 qty:</label><input type="text" name="ProductQuantities[728][value]" id="ga-50x70" value="0" /></div> <input type="hidden" name="ProductQuantities[728][name]" value="GA 50x70" /> 
<button type="submit">Next</button> 
</form> 
<button data-close="" class="refreshId" type="submit">Close</button> 

app.js : 여기

$(document).ready(function() { 

    $(document).foundation(); 

    $("body").on("click", "a[data-open]", function(e) { 
     e.preventDefault(); 

     var $this = $(this); 
     var modalName = $this.data("open"); 
     var $modal = $("#" + modalName); 
     var $target = $modal.children(".modalContent"); 
     var requestUri = $(this).attr('href'); 
     var requestData; 
     var method; 
     if($this.attr('data-request')) { 
      requestData = $(this).data("request"); 
      method = 'POST'; 
     } else { 
      requestData = ""; 
      method = 'GET'; 
     } 


     // Load content: 
     var request = $.ajax({ 
      url: requestUri, 
      method: method, 
      data: requestData, 
      dataType: "html", 
      timeout: 60000 
     }); 

     request.done(function(response) { 
      $target.html(response); 
      $target.foundation(); 
     }); 
    }); 
}); 

당신이 적어도 내 브라우저에서 (동작을 테스트 할 수 있습니다 파이어 폭스 & Chrome) : [http://codepen.io/repeat_spacer/pen/bBRZKd][1]. 버튼에서 공개 팝업 창을 열고 텍스트 필드의 값을 변경하십시오. 마우스 클릭으로 필드를 입력 할 수 없습니다.

누군가가 내게 문제를 지적하거나 계속 추적 할 수 있기를 바랍니다.

가 UPDATE :

내가 modalform.html 코드를 청소 (나는 아약스로드 다음 형태가 있고, 일부 아약스로드 새로운 형태의 물건을 공개있다). 이제는 "다음 양식로드"기능이 작동하지 않지만 다른 한편으로는 "포커스 손실"동작이 사라집니다.

modalform_updated.html :

<h3 class="modalheader">PopupForm</h3> 
<form method="post" accept-charset="utf-8" class="ajaxModal" action="submitmodal.php"> 
    <div style="display:none;"><input type="hidden" name="_method" value="POST" /></div> 
    <div class="input text"><label for="p1qty">Product 1 qty:</label><input type="text" name="ProductQuantities[p1][value]" id="p1qty" value="0" /></div> 
    <div class="input text"><label for="p2qty">Product 2 qty:</label><input type="text" name="ProductQuantities[p2][value]" id="p2qty" value="0" /></div> 
    <div class="input text"><label for="p3qty">Product 3 qty:</label><input type="text" name="ProductQuantities[p3][value]" id="p3qty" value="0" /></div> 
    <button type="submit">Submit</button> 
</form> 
<button data-close="" class="refreshId" type="submit">Close</button> 

업데이트 codepen : http://codepen.io/repeat_spacer/pen/MbrYvj

업데이트 2 : 여기

는 형태 I 데이터 오픈 (재단은 오픈 속성을 밝히기) 한 이유입니다 속성. 폼의 필드를 클릭하면 Foundation Open 이벤트가 트리거되는 원인이되는 것 같습니다. 그래서 빨리 수정하면 더러운 방식으로 필터링됩니다. 여기

내가 갖고 싶어 멀티 팝업 기능을 피들러입니다 : 사실 그 http://codepen.io/repeat_spacer/pen/JbMdPm 난 당신이 여기에 실행중인 문제를 생각

+0

아래 투표의 이유는 무엇입니까? –

+2

질문이 http://stackoverflow.com/help/how-to-ask 잘 따르지 않기 때문에 아마도 – GillesC

+0

@ GillesC 그럼 이제 괜찮습니까? –

답변

2

좋아 내가 지금 일하고있어 트리거하기 전에 이미 열려있는 공개 여부를 확인하기 위해 뭔가.마찬가지로 문제는 재단 이벤트 바인딩에 있었다.

양식 속성에 data-open="modalName"을 추가했을 때 Ajax가 다음 모달 페이지를로드 할 것을 나타 내기 위해 양식 속성에 추가했습니다 (첫 번째 단추에서 모달 창을 표시해야하는 것처럼). .)

이제 data-open이 양식 속성에있을 때 양식의 항목을 클릭 할 때마다 공개 이벤트가 표시됩니다. 이 이벤트는 모든 공개를 닫은 다음 ID가 공개 된 데이터 공개 속성을 열어 초점을 잃게 만듭니다.

모달 이름을 data-modalname으로 저장했으며 모든 것이 예상대로 작동합니다.

HTML 수정 :

<form method="get" accept-charset="utf-8" class="ajaxModal" data-showloading="1" data-modalname="this" action="modalform2.html"> 

그리고 거기에서 모달 이름을 읽을 JS에 이벤트 핸들러를 변경.

작동 코드 펜 : http://codepen.io/repeat_spacer/pen/QGazJj

+1

나는 더 많은 것을 파헤 쳤고, 나는 당신의 추론이 정확하다고 생각한다. 양식에서 data-open은 양식을 열 때 모든 클릭을 발생시킵니다. – kball

1

(1 팝업 모달 아약스로드 다음 같은 모달의 내용을 팝업)입니다 이벤트 처리기를 body에 배치하면 페이지의 내용을 클릭 할 때마다이 이벤트 핸들러가 실행됩니다. 노출 내에서 클릭하면 공개 모드를 다시 열어 이벤트 핸들러가 트리거되므로 포커스가 엉망입니다.

이 문제를 해결하려면 클릭 처리기를 페이지의 다른 항목에 할당하십시오. 당신은 정말 몸에 그것을 가지고 싶은 경우 또는, 당신이 그것을 ... 라

$("body").on("click", "a[data-open]", function(e) { 
    if($('body').hasClass('is-reveal-open')) { return; } 
    // rest of handler 
} 
+0

제안 해 주셔서 감사합니다. .on을 사용하여 이벤트 핸들에서 본 내용은 일반적으로 본문 또는 전체 문서에 바인딩됩니다. 아이디어는 동적 추가 요소 (문서로드 후 JavaScript 등)에 바인딩된다는 것입니다. 이 이벤트는 body 태그 안에 "data-open"데이터 속성이있는 앵커를 클릭 할 때만 트리거됩니다. 그래서 revel의 필드가 클릭되면 발사되지 않습니다. –