代码之家  ›  专栏  ›  技术社区  ›  Kristen

在JQuery中切换可见性

  •  4
  • Kristen  · 技术社区  · 16 年前

    我有一条提醒信息 [#CheckboxReminder] “勾选此复选框表示您已检查数据”。

    [#IsConfirmed] 勾选,然后将其还原为原始状态(如果未选中)。

    页面呈现时,提醒消息设置为可见或隐藏类;如果用户最近将其数据标记为“已选中”,则消息将被隐藏(但如果用户愿意,欢迎再次选中此框)

    toggle() 可以这样做,但我读到这取决于 #CheckboxReminder 样式被设置为表示可见性,我也读过 Toggle() #IsConfirmed 复选框-例如快速双击。也许我应该 toggle(FunctionA, FunctioB) 让FunctionA设置复选框状态,FunctionB取消设置它-而不是允许单击设置它?

    如果有用,下面是一个HTML的示例:

    <p>When the data above is correct please confirm 
        <input type="checkbox" id="IsConfirmed" name="IsConfirmed"> 
        and then review the data below</p>
    ...
    <ul>
    <li id="CheckboxReminder" class="InitHide OR InitShow">If the contact details
        above are correct please make sure that the CheckBox is ticked</li>
    <li>Enter any comment / message in the box above</li>
    <li>Then press <input type="submit"></li></ul>
    
    1 回复  |  直到 16 年前
        1
  •  9
  •   quadfinity Gabriele Petrioli    10 年前

    在显示/隐藏消息之前,只需检查复选框是否确实更改了值。

    $('#isConfirmed').click(
      function(){
        if ( $(this).is(':checked') )
          $('#CheckboxReminder').show();
        else
          $('#CheckboxReminder').hide();
      }
    );
    

    每次单击复选框时,上面的内容都会触发,但由于我们首先检查复选框是否确实选中,因此将避免 假阳性