切换 JQuery 中的可见性

发布于 2024-08-19 22:32:25 字数 1119 浏览 1 评论 0原文

我可以在 JS 中做到这一点,但我开始使用 JQuery 并且更愿意为此开发技能。

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

然后,我想在勾选复选框 [#IsConfirmed] 时隐藏提醒消息,如果随后取消选中,则将其恢复到原始状态。

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

我相信 JQuery toggle() 可以做到这一点,但是我读到这取决于 #CheckboxReminder 样式设置来指示可见性的方式,而且我还读到 Toggle() 可能与 不同步>#IsConfirmed 复选框 - 例如快速双击。也许我应该有 toggle(FunctionA, FunctioB) 并让 FunctionA 设置复选框状态,而 FunctionB 取消设置它 - 而不是允许 Click 设置它?

请问最好的编码方式是什么?

如果有用,这里是 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>

I can do this in JS, but I'm beginning to use JQuery and would prefer to develop skills for that.

I have a reminder message [#CheckboxReminder] "Tick this checkbox to indicate you have checked your data".

I then want to hide the reminder message when the checkbox [#IsConfirmed] is ticked, and restore it to original state if it is then unchecked.

The page will be rendered with the reminder message set to a class of either Visible or Hidden; if the user has recently marked their data as "checked" the message will be hidden (but user is welcome to check the box again if they wish)

I believe that JQuery toggle() can do this, but I read that it depends on the way that the #CheckboxReminder style is set to indicate visibility, and I have also read that Toggle() could get out of sync with #IsConfirmed CheckBox - e.g. rapid double clicking. Maybe I should have toggle(FunctionA, FunctioB) and have FunctionA set the checkbox state, and FunctionB unset it - rather than allowing the Click to set it?

What's the best way to code this please?

In case useful here's an example of what the HTML might look like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳根太软 2024-08-26 22:32:25

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

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

每次单击复选框时都会触发上面的代码,但由于我们首先检查复选框是否确实被选中,因此可以避免误报

Just check if the checkbox has indeed changed value before showing/hiding the message.

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

The above will fire each time the checkbox is clicked, but since we first check if the checkbox is indeed checked or not it will avoid false positives.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文