2011-01-27 15 views
1

次の形式:ユーザーがチェックボックスのチェックを外したことを検出するにはどうすればよいですか?

<form action="x.php" method="get" id="myForm">Subscribe: 
<div id="radioButtonsWithAdds"> 
    <input type="radio" name="group1" value="one">One month 2,99$ 
    <input type="radio" name="group1" value="two"> Two months 5,98$</div><br> 
    <input type="checkbox" name="option1" value="withAdds" checked> with adds 
    <img src="next.png" border="0" alt="enviar" onclick="document.getElementById('myForm').submit();"> 
    </form> 

は、サブスクリプションからの最初の部分です。ここでは、ユーザーは1ヶ月または2ヶ月のサブスクリプションを選択できます。

ユーザーが追加を受け取ることに同意する場合、チェックボックスをオンにしておくと、そのすべてが表示され、次のボタンを押すことができます。

ただし、ユーザーが[追加する]チェックボックスをオフにすると、別の価格の別のラジオボタンを含む#radioButtonsWithを追加する場所にdivが表示されます。

送信ボタンを押さずにチェックボックスが表示されていることを検出するにはどうすればよいですか?

答えて

13
$('#yourcheckbox').change(function() { 

    if (! this.checked) { 
     // It is not checked, show your div... 

    } 

}); 
+1

どうもありがとう!!! – user523129

+0

ありがとう、それは純粋なJSとの唯一の答えのために –

0

あなたはイベントリスナーを結合して、そこに状態を確認する必要があります。

$('#radioButtonsWithAdds').find('input:checkbox').bind('change', function() { 
    if(this.checked) { 
    } 
    else { 
     // the user unchecked the checkbox! 
    } 
}); 
0

ここでは、この

  <TD width=550 align=left> 
       <input type="checkbox" name="adesao" value="sim" id="parte1" onClick="mostra()" /> Sim 
       <div id="sumula" style="display:none"> 
        <input type="radio" name="conteudo_sumula" value="1" /> <small><i>1x</i></small> 
        <input type="radio" name="conteudo_sumula" value="2" /> <small><i>2x</i></small> 
        <input type="radio" name="conteudo_sumula" value="3" /> <small><i>3x</i></small> 
       </div> 
      </TD> 

を使用してみてくださいJS機能に

function mostra(){ 
    if (document.getElementById("sumula").style.display != "none"){ 
     document.getElementById("sumula").style.display = "none"; 
    }else{ 
     document.getElementById("sumula").style.display = "block"; 
    } 
} 
+0

+1を働いた:) –

1

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
    <head> 
    <meta name="generator" content="test"/> 
    <title></title> 
    </head> 
    <body> 
    <form action="x.php" method="get" id="myForm" name="myForm"> 
     Subscribe: 
     <div id="radioButtonsWithOutAdds" style="display:none;"> 
     <input type="radio" name="group1" value="one">One month 1,99$ 
     <input type="radio" name="group1" value="two"> Two months 2,98$ 
     </div> 
     <div id="radioButtonsWithAdds"> 
     <input type="radio" name="group1" value="one">One month 2,99$ 
     <input type="radio" name="group1" value="two"> Two months 5,98$ 
     </div><br> 
     <input type="checkbox" name="option1" value="withAdds" checked> with adds 
     <img src="next.png" border="0" alt="enviar" onclick= 
     "document.getElementById('myForm').submit();"> 
    </form> 
    </body> 
</html> 
を行きます10の

JS

$(document).ready(function(){ 
    $("#myForm > input[type='checkbox']").click(function(){ 
     if(!$(this).is(':checked')) 
     { 
      $("#radioButtonsWithOutAdds").show(); 
      $("#radioButtonsWithAdds").hide(); 
     } 
     else 
     { 
      $("#radioButtonsWithOutAdds").hide(); 
      $("#radioButtonsWithAdds").show(); 
     } 
    }) 
}) 

ここでは簡単な例です: http://jsfiddle.net/efhHF/2/

1

ます。また、このような何かを行うことができは:

$('input[name=subscribe]').change(function() { 
    if ($(this).is(':checked')) { 
    // the user selected the checkbox 
    } else { 
    // the user de-selected the checkbox 
    } 
}); 
関連する問題