2012-08-06 17 views
2

나는 여기서 발견 한 것들을 기반으로 몇 시간 동안 다른 방법을 시도 해왔다. 누군가 나를 올바른 방향으로 조종 해 줄 수 있습니까? 확인란을 선택하거나 선택 취소 할 때 확인을 던지려면 확인란을 선택하거나 사용자 선택에 따라 선택을 취소합니다. 고맙습니다!체크 박스 체크 박스 체크 박스

$('input:checkbox').click(function(){ 
    var checked = $("input:checked").length; 
    if (checked == 0){ 
if(confirm('Are you sure you want to discontinue this program?')){ 
    (':checkbox:checked').removeAttr('checked'); 
} 
    } else 

    { 
if(confirm('Are you sure you want to resume use of this program?'))    
(':checkbox:checked').attr('checked'); 
    } 

HTML

리스너가 확인란을 아직 선택 또는 선택되어 실행
<table> 
    <tr> 
    <td><input name="input" type="checkbox" value="" /></td> 
    <td>Because We Care</td> 
    </tr> 
     <tr> 
        <td><input name="" type="checkbox" value="" /></td> 
        <td>Some Unused Program</td> 
        </tr> 

+0

http://stackoverflow.com/questions/5944889/intercepting-checkbox-click-to-confirm-the-change 질문을 게시하기 전에 제대로 구글하시기 바랍니다 :이 당신을 위해 무엇을 찾고있는 정확하게해야한다 –

답변

1

. 당신이해야 할 것은 :

  • 사용자가 사용자가
6

코드에서 일부 구문 & 로직 결함이 있습니다 수행 된 내용을 취소 확인하지 않는 경우에 아무것도에게

  • 을하지 않는다 확인하면, 시도 이 :

    $('input:checkbox').click(function(){ 
        var checked = $(this).is(':checked'); 
        if(checked) { 
         if(!confirm('Are you sure you want to resume use of this program?')){   
          $(this).removeAttr('checked'); 
         } 
        } else if(!confirm('Are you sure you want to discontinue this program?')){ 
         $(this).attr("checked", "checked"); 
        } 
    }​);​ 
    

    JSFIDDLE

  • +0

    빠른 응답과 설명에 감사드립니다! 그러나 확인란을 선택했을 때 확인 상자가 나타나지 않습니다. 내가 잘못하고있는 다른 것이 있습니까? – squirc77

    +0

    다시 신고하기 ... 감사합니다! 실제로 작동하도록했습니다. 이 오류를 한 번에 한 줄의 코드로 제거하여 결국 오류가 발생했습니다. 나를 걸러내는 것은 모든 대괄호입니다! 나는 당신의 도움에 감사드립니다! – squirc77

    0
    <!doctype html> 
    <html> 
    <input id="chkTest" type="checkbox" /> 
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script> 
    <script type="text/javascript"> 
        $("#chkTest").change(function(evt){ 
         var attr = $(evt.target).attr("checked") 
         if (typeof attr !== 'undefined' && attr !== false){ 
          alert("Checked"); 
         } 
         else{ 
          alert("Not Checked"); 
         } 
        }); 
    </script> 
    </html> 
    
    +0

    도움을 주셔서 감사합니다. 위의 어느 것으로도 확인 된 상자를 얻을 수는 없지만. blang32 이상의 코드를 이해하고 있습니다. 모든 설명이 감사하겠습니다. js와 jquery 모두에 매우 새로운 ... 두 가지를 배우기 위해 본격적으로 노력하고 있습니다. 힘들어! :디 – squirc77

    관련 문제