【问题标题】:How to detect that a user has unchecked a checkbox?如何检测用户未选中复选框?
【发布时间】:2011-01-27 12:13:49
【问题描述】:

如下形式:

    <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>

是订阅的第一部分。在这里,用户可以选择一个月或两个月的订阅。

如果用户同意接收添加,勾选复选框,就可以了,他/她可以点击下一步按钮就可以了。

但如果用户取消选中“添加”复选框,则会在 #radioButtonsWithAdds 的位置显示一个 div,其中包含另一组价格不同的单选按钮。

如何在不点击任何提交按钮的情况下检测到复选框?

【问题讨论】:

    标签: jquery checkbox form-submit detect unchecked


    【解决方案1】:
    $('#yourcheckbox').change(function() {
    
       if ( ! this.checked) {
           // It is not checked, show your div...
    
       }
    
    });
    

    【讨论】:

      【解决方案2】:

      你也可以这样做:

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

      【讨论】:

        【解决方案3】:

        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>
        

        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/

        【讨论】:

          【解决方案4】:

          您需要绑定一个事件监听器并检查那里的状态。

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

          【讨论】:

            【解决方案5】:

            尝试使用这个

                      <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";
                }
            }
            

            【讨论】:

              猜你喜欢
              • 2012-11-12
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-01-12
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多