2012-02-03 2 views
0

사용자 작업을 확인하는 확인 경고 메시지가 나타납니다. "취소"를Jquery if if statement confusion

function disableSubmitButton() { 
    jQuery("#submitButton").attr("disabled", "true"); 
    jQuery("#rent_space").submit(); 
} 

, 다음 : "확인"을 클릭에

, 그것은 다음 코드를해야한다.

if (!confirm('Are you sure you want to proceed ?')) { 
    return false; 
} 

그러나 현재 내 코드가 형식화되는 방식으로 어떤 단추가 눌러 졌는지에 관계없이 첫 번째 코드 블록이 실행됩니다.

이 코드는 코드를 움직이는 것만으로도 충분하다고 생각합니다. 그러나 전례 시도가 모두 실패한 것처럼 올바르게 수행하는 방법은 확실하지 않습니다. 어떤 도움을 주셔서 감사합니다. 감사 !

전체 코드 :

<script> 
    jQuery(document).ready(onDocumentReady); 
     function onDocumentReady() { 

      jQuery("#submitButton").click(disableSubmitButton); 

      jQuery('#submitButton').click(function() { 
       if (!confirm('Are you sure you want to proceed ?')) { 
        return false; 
       } 
      }); 

     } 
     function disableSubmitButton() { 
      jQuery("#submitButton").attr("disabled", "true"); 
      jQuery("#rent_space").submit(); 
     } 
</script> 
+0

#submitButton이란 무엇입니까? 마크 업을 표시 할 수 있습니까? – Vinnyq12

답변

3

당신은 시도 할 수 있습니다 :

//jQuery("#submitButton").click(disableSubmitButton); 

jQuery('#submitButton').click(function() { 
if (!confirm('Are you sure you want to proceed ?')) { 
    return false; 
} 
else 
{ 
    disableSubmitButton(); 
} 
}); 
1

당신이 정말로 버튼을 제출하지 않도록해야합니까? 다른 클릭에서 false를 반환하면 충분합니다.

<script> 
    jQuery(document).ready(onDocumentReady); 
     function onDocumentReady() { 

      jQuery('#submitButton').click(function() { 
       if (!confirm('Are you sure you want to proceed ?')) { 
        return false; 
       } 
      }); 

     } 

</script> 

그러나 내가 대신 제출 이벤트에 바인딩 것 :

<script> 
    jQuery(document).ready(onDocumentReady); 
     function onDocumentReady() { 

      jQuery('#rent_space').submit(function() { 
       if (!confirm('Are you sure you want to proceed ?')) { 
        return false; 
       } 
      }); 

     } 

</script> 
+0

양식을 제출할 때만 사용 중지한다고 생각합니다. 함수 이름은 함수가 수행하는 것만 반영하지 않으므로 약간 혼란 스럽습니다. –

+0

+1 나는 더 나은 제출물을 좋아한다. –

1

당신은 하나의 이벤트 리스너가 필요합니다. 조건은 리스너 본문에서 처리 할 수 ​​있습니다.

jQuery("#submitButton").click(function(e) { 
    e.preventDefault(); 
    if(confirm("...")) { 
    jQuery("#submitButton").attr("disabled", "true"); 
    jQuery("#rent_space").submit(); 
    } 
}); 

NB. 이벤트의 기본 동작을 방지하려면 return false 대신 e.preventDefault()을 사용해야합니다.

+0

e.preventDefault()는 매력처럼 작동했습니다. 고맙습니다 ! – JFFF

1

하리의 대답은 정확합니다. 나는 (약간) 더 관용적을 선호 : 나는 그것을 가지고

jQuery("#submitButton").click(function() { 
    if (confirm("Are you sure you want to proceed?")) { 
     disableSubmitButton(); 
    } else { 
     return false; 
    } 
}); 
0

편집 편집는 귀하의 제안에 감사를 작동 할 수 있습니다. 고맙습니다. :)

Heres는 효과가있는 코드입니다.

 <script> 
     jQuery(document).ready(onDocumentReady); 
      function onDocumentReady() { 

       jQuery('#submitButton').click(function() { 
        if (!confirm('Are you sure you want to proceed ?')) { 
         return false; 
        } 
        else{ 
         jQuery("#submitButton").click(disableSubmitButton); 
        } 
       }); 

      } 
      function disableSubmitButton() { 
       jQuery("#submitButton").attr("disabled", "true"); 
       jQuery("#rent_space").submit(); 
      } 
    </script> 
+0

이렇게하지 마십시오 :'jQuery ("# submitButton"). click (disableSubmitButton);'. 그것은 다른 클릭 이벤트를 첨부하고 호출하지 않는 것입니다. 이미 해당 버튼에 대한 클릭 이벤트가 있습니다. 나는 놀랍다 - 심지어 양식이 제출되지 않는다는 것인가? –

+0

죄송합니다. 나쁜 사본 파스타,이 선은 실제로 else { disableSubmitButton(); } 내 코드 :) – JFFF

+0

구현하기로 선택하지 않은 답변을 받아 들였지만 괜찮습니다. –