2012-01-28 5 views
0

입력 유형 체크 박스를 클릭하려고합니다. 클릭하면 AJAX 호출이 수행됩니다. 나는 청취자를 세웠지 만 아무 일도 일어나지 않는다 ... 방화범도 보이지 않는다.JQuery Listener가 듣지 않음

코드 : 당신은 이벤트 리스너가 설정하지 않아도

<form method="post" action="profile/<?php echo $_SESSION['usern']; ?>/settings"> 
    <p><input type="checkbox" id="profile_visible" name="profile_visible" /> Show Profile<span id="resultProfileVisible"></span></p> 
    </form>  

감사

+2

어떤 청취자가 추천 해 주나요? 나는 오직 하나만 볼 수 있습니다 - 준비된 문서. 그것이 당신이 의미하는 것입니까? –

답변

1

:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js{API removed}"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
alert('test'); 
if ($('#profile_visible:checked').val() !== null) { 

     $.ajax({ 
      url: 'inc/profileVisible.php', 
      success: function(data) { 
      $('#resultProfileVisible').innerhtml="success"; 
      alert('Load was performed.'); 
      } 
     }); 
} 
    } 
    </script> 

... 그리고 문서의 본문에. on과 결합 이벤트 핸들러 (jQuery를> 1.7) 또는 bind (jQuery를 < 1.7) 또는 jQuery의 각종 단축키 방법 (같은 .change 또는 .click) 또한

$(document).ready(function() { 
    $("#profile_visible").change(function() { 
     if (this.checked) { 
      $.ajax({ 
       url: 'inc/profileVisible.php', 
       success: function(data) { 
        $('#resultProfileVisible').html("success"); 
        alert('Load was performed.'); 
       } 
      }); 
     } 
    }); 
}); 

는 요소의 HTML 콘텐츠 때를 설정하는 대신 .innerHtml.html()를 사용 jQuery 객체 사용.

예 :http://jsfiddle.net/andrewwhitaker/8x6h8/1/

+0

도와 주셔서 감사합니다. 도움을 주신 덕분에 – mandalorianwarrior

1

유일한 리스너가 준비 DOM, 그래서 한 번만 실행됩니다.

코드에 몇 가지 문제가 있습니다. 아래를 참조하십시오.

$(document).ready(function() { 
    alert('test'); 
      // I assume you just want to see if it is checked 
    if ($('#profile_visible').is(':checked')) { 

     $.ajax({ 
      url: 'inc/profileVisible.php', 
      success: function (data) { 
        // jQuery has .html(), not innerhtml 
       $('#resultProfileVisible').html("success"); 
       alert('Load was performed.'); 
      } 
     }); 
    } 
}) // <-- was missing closing parentheses 
+0

. 많이 감사 ... 나는 jQuery 4 년 틈새에 있었고 단지 그것에 다시 들어가려고 노력했다. 자연스럽게 조언이 도움이되었습니다. 이제 페이지로드에 경고 "테스트"가 표시됩니다. 그러나 체크 박스를 체크하면 $ ajax가 실행되지 않습니다. PHP 스크립트 경로가 맞습니다. Firebug는 "Script"아래에서 다음과 같이 알려줍니다. 제한된 URI에 대한 액세스가 거부되었습니다. 다시, ajax 서버 측 스크립트에 대한 경로가 정확합니다. 조언? 감사합니다 – mandalorianwarrior

+0

@ user1175528 : 디렉토리 구조를 잘 모르지만 절대 경로가 필요합니까? ' '/ inc/profileVisible.php' '. 클릭 할 때마다 실행이 필요하면 [Andrew Whitaker의 대답] (http://stackoverflow.com/a/9047445/1106925)을 참조하십시오. –

0

변화 내가 리스너를 보이지 않아요이

$(document).ready(function(){ 
$('#profile_visible').change(function(){ // handler when the checkbox is toggled 
    if($(this).attr('checked')) { // check if it is checked 
     $.ajax({ 
      url: 'inc/profileVisible.php', 
      success: function(data) { 
      $('#resultProfileVisible').html("success"); // change here 
      alert('Load was performed.'); 
      } 
     }); 
    } 
    }); 
}); 
+0

'if ($ (this) .attr ('checked')'=>'if (this.checked)' – gdoron

+0

네가 맞다. 첫 번째는 jquery이고 다른 하나는 순수한 자바 스크립트이다. –

+0

대단히 고마워,이 코드를 구현하고 아름답게 작동한다. 내 실수를 많이 본다 .. 많이 감사하겠습니다. – mandalorianwarrior

0

같은 코드입니다. 시도해보십시오.

$(document).ready(function() { 
    $('#profile_visible').change(function() { 
     if ($(this).attr('checked')) { 
      $.ajax({ 
       url: 'inc/profileVisible.php', 
       success: function(data) { 
       $('#resultProfileVisible').html("success"); // change here 
        alert('Load was performed.'); 
       } 
      }); 
     } 
    }); 
}); 
+1

'if ($ (this) .attr ('checked')'=>'if (this.체크)' – gdoron

+0

대단히 감사합니다 .. 나는 당신의 게시물에서 배웠습니다. 감사 – mandalorianwarrior

관련 문제