2011-09-24 9 views
5

은 #button에 대한 클릭을 수신해야하지만 작동하지 않으며 나를 미치게합니다.jQuery 리스너가 작동하지 않음

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> 
<script type="text/javascript"> 
    $('#button').click(function() { 
     alert('OK!'); 
    }); 
</script> 

과 HTML은 :

<input id="button" type="button" value="OK" /> 

이 이상하다. 어떤 도움도 환영합니다!

답변

13

<script type="text/javascript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"> 
</script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $('#button').click(function() { 
     alert('OK!'); 
    }); 
}); 
</script> 


OR 
<script type="text/javascript"> 
    function on_click() { 
      alert("OK !!"); 
    } 
    $(document).ready(function() { 
     $("#button").click(on_click); 
    }); 
</script> 

당신이 여기이

+0

ca n이 논리를 전체 'functions.js'파일에 적용합니까? –

+0

예 js 파일 내부에도 적용 할 수 있습니다. –

+0

$ (document) .ready (function() {FUNCTIONS etc})에있는 모든 함수 문서를 래핑하려고 시도했습니다. 작동하지 않습니다. –

2

에서 몇 가지 아이디어를 얻을 희망 document.ready 함수 내에서 코드를 작성하는 것은 같은 코드가 보일 것입니다 무엇 : 여기 JQuery와 문서의

<script type="text/javascript" 
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"> 
</script> 
<script type="text/javascript"> 
    // Now that jquery is include, place the code to wire up the button here 
    $(function(){ 
     // Once the document.onload event fires, attach the click 
     // event handler for the button 
     $('#button').click(function() { 
      alert('OK!'); 
     }); 
    }); 
</script> 
<input id="button" type="button" value="OK" /> 

그 이것과 관련있다 : http://docs.jquery.com/How_jQuery_Works

+0

원본 코드가 작동하지 않는 이유에 대한 더 나은 설명과 how $ (function() {}); 공장. – gcastro

관련 문제