2013-10-17 3 views
2


jQuery 자체가 아닌 수동으로 이벤트를 트리거하고 싶습니다.jQuery가 자동으로 체크 박스 이벤트를 트리거합니다.

$('.commonCheck').change(function() { 
      if ($(".cashierCheck").attr("checked")) { 
       $('.cashierInput').fadeIn(); 
       console.log("________________hr") 
      } 
      else{ 
       $('.cashierInput').fadeOut(); 
       console.log("_______________uncjecked") 
      } 
} 

내 체크 박스 : 내가 수동으로 검사 할 때

<input type="checkbox" name="cashierFilter" id="cashierFilter" class="cashierCheck commonCheck" class="mrL0" /> 

는/변경 이벤트가 아무 문제없이 실행됩니다의 체크 박스를 체크 해제하고 다음
내 문제 더 나은 이해합니다. 는하지만 난 어떻게 든이 실행할 때 이벤트가 발생되고 싶지 :

$('#cashierFilter').prop('checked','true'); 

답변

2

데모http://jsfiddle.net/ga9nn/

당신은 프로그래밍 방식으로 이벤트를 발생하는 trigger를 사용할 수 있습니다 또한

$('#cashierFilter').prop('checked', true).trigger('change'); 

, 당신이해야을 is을 사용하여 요소의 속성을 확인하고 변수에 선택기를 캐시하여 성능을 향상시킬 수 있습니다.

$('.commonCheck').change(function() { 
    var $cashierCheck = $(".cashierCheck"); 
    if ($cashierCheck.is(":checked")) { 
     $cashierCheck.fadeIn(); 
     console.log("________________hr") 
    } 
    else { 
     $cashierCheck.fadeOut(); 
     console.log("_______________uncjecked") 
    } 
} 
+1

Bruvoo이 데모를 추가, 그것을 자유롭게 사용, 미안 내가 귀하의 게시물을 요 감동! ':)'어쨌든 일어나. "데모 *는'document .ready'와 같습니다 –

+0

완벽한 답변 !! 감사합니다 둘 다 –

+1

@Tats_innit 좋은 엄마, 다른 엄마의 동생입니다. –

2

사용 jQuery를에 최적의 솔루션 "입니다"

$('.commonCheck').change(function() { 
    if ($(".cashierCheck").is(":checked")) { 
       $('.cashierInput').fadeIn(); 
       console.log("________________hr") 
      } 
      else{ 
       $('.cashierInput').fadeOut(); 
       console.log("_______________uncjecked") 
      } 
}); 

http://jsfiddle.net/PpcLe/

관련 문제