mootools 根据是否选中复选框显示/隐藏 div

发布于 2024-09-24 07:52:51 字数 678 浏览 9 评论 0原文

我有一个复选框,我想根据是否单击该复选框来在表单中显示字段集?即,如果选中该复选框,则不显示字段集,如果未选中该复选框,则显示字段集。

我的标记看起来像这样,

<fieldset class="toplined">
<label>Keep Image</label>
<input type="checkbox" name="update_image[]" value="1" id="toggle" checked='true'/>
</fieldset>

<fieldset class="toplined toggle_slide">
<label>Image:</label>
<input type="file" name="article_image[]">
</fieldset>

带有toggle_slide类的字段集是我想要显示/隐藏的字段集,我目前有这个mootools脚本,

$('toggle').addEvent('click', function(e){
    e = new Event(e);
    $$('.toplined toggle_slide').toggle();
    e.stop();
});

但这只会导致错误。

I have a checkbox I wanting to show a fieldset in a form dependent on whether or not the checkbox is clicked or not? i.e if the checkbox is checked don't show the fieldset and if it is not show the fieldset.

My markup looks like this,

<fieldset class="toplined">
<label>Keep Image</label>
<input type="checkbox" name="update_image[]" value="1" id="toggle" checked='true'/>
</fieldset>

<fieldset class="toplined toggle_slide">
<label>Image:</label>
<input type="file" name="article_image[]">
</fieldset>

the fieldset with the class toggle_slide is the one I want to show/hide, I currently have this mootools script

$('toggle').addEvent('click', function(e){
    e = new Event(e);
    $('.toplined toggle_slide').toggle();
    e.stop();
});

however this just results in errors.

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

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

发布评论

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

评论(2

给我一枪 2024-10-01 07:52:51

解决方案

这是一个快速解决方案。如果您有兴趣或需要有关如何使其发挥作用的更多信息,请继续阅读!

$('toggle').addEvent('click', function(e){
    if(this.checked)) {
        $('.toggle_slide').toggle();
    }
});

事件

在 Mootools 中,所有元素都有 addEvent 方法,该方法接受两个参数。第一个参数是事件的类型(例如单击),第二个参数是该事件发生时调用的函数。该函数有一个自动传递给它的参数,即事件本身。因此,在 addEvent 方法内创建新事件是一个很大的错误,因为新事件与调用 addEvent 方法函数的事件不同。您可以在函数参数列表中指定默认事件参数的名称,然后正常使用它。因此,在您的代码中,e 已经存在,因为这就是您在这段代码中调用的事件: function(e){ 以及所有行 e = new Event(e ); 的作用是用空白默认事件覆盖此参数。这几乎肯定不是您想要的。

即使 JavaScript 不存在,许多操作也会在事件发生时发生。例如,当提交表单时,提交事件告诉浏览器收集该表单的数据并将其发送到表单的操作。另一个例子是,当您单击链接时,单击事件会告诉浏览器跟随该链接。这些被称为默认事件。但是,当我们将 addEvent 方法附​​加到链接时,我们希望发生我们自己的操作,而不是浏览器的默认操作。我们可以通过使用事件对象的 stop 方法来防止这些默认操作的发生。

由于 e 是您的事件对象,因此 e.stop() 将防止发生任何默认操作。但是,在这种情况下,复选框上的单击事件的默认操作是选中或取消选中该复选框。 e.stop() 可能会阻止这种情况发生(我没有测试过),但充其量不会做任何事情。因此,它要么会让您的用户感到困惑,要么什么也不做,所以我建议您将其删除。

最后,在 addEvent 函数中,您可以使用 this 关键字来引用事件所附加到的元素。

选择器

Mootools 有一个非常先进的选择器系统,它允许您选择几乎任何您想要的东西。您需要选择有两个类的字段集。但是,您选择的选择器 $$('.toplinedtoggle_slide') 表示“查找具有 toplined 类的元素,然后在该元素内查找具有标签toggle_slide 的元素”。相反,您应该使用 $$('.toggle_slide')

元素

您可以使用 selected 属性检测复选框是否被选中。如果选中复选框,则返回 true,否则返回 false。在您的情况下,因为复选框是您要添加事件的元素,所以您可以使用 this.checked 来确定它是否被选中。

提示:如果您使用 Firebug,则可以使用 console.log 在控制台中打印任何元素。这将显示该元素在该时间点拥有的所有属性和方法。这将很快向您显示诸如检查之类的内容。

此外,Mootools 分为两个组件:Mootools core 和 Mootools more。如果您有 Mootools 中包含的 Element.Shortcuts,则仅在 Elements 上定义切换方法更多或者如果您自己编写了该方法。否则你会得到一个错误。

Solution

Here is a quick solution. If you are interested or require more information on how to get this to work, read on!

$('toggle').addEvent('click', function(e){
    if(this.checked)) {
        $('.toggle_slide').toggle();
    }
});

Events

In Mootools, all elements have the addEvent method which takes two arguments. The first argument is the type of event (e.g. click) and the second is a function to call when that event occurs. This function has an argument automatically passed to it which is the event itself. Therefore, creating a new event inside an addEvent method is a big mistake as the new event won't be the same event as that which called the addEvent method's function. You can specify the name of the default event argument in the function parameter list and then use it as normal. Consequently, in your code, e already exists, as that is what you have called the event in this piece of code: function(e){ and all the line e = new Event(e); does is overwrite this parameter with a blank default event. This is almost certainly not what you want.

Many actions occur when events happen even when JavaScript isn't present. For example, when a form is submitted, the submit event tells the browser to collect the data for that form and send it to the form's action. Another example, is when you click on a link, the click event tells the browser to follow that link. These are known as default events. However, when we have attached the addEvent method to, say, a link, we want our own actions to occur, not the browser default. We can prevent these default actions from occurring by using the stop method on event objects.

Since e is your event object, e.stop() will prevent any default actions from occurring. However, in this case, the default action for a click event on a checkbox is to check or uncheck that checkbox. e.stop() may prevent this from happening (I haven't tested it) but at best will do absolutely nothing. Therefore, it will either confuse your users or do nothing so I suggest you remove it.

Finally, in an addEvent function, you can use the this keyword to refer to the element which the event is attached to.

Selectors

Mootools has a very advanced selector system which allows you to select almost anything you want. You need to select your fieldset which has two classes. However, the selector you have chosen, $$('.toplined toggle_slide'), says "Find elements with the class toplined and then inside that element, find elements with the tag toggle_slide". Instead you should use $$('.toggle_slide')

Elements

You can detect whether or not a checkbox is checked with the checked property. This will return true if a checkbox is checked and false otherwise. In your case, because the checkbox is the element you are adding the event to, you can use this.checked to determine whether or not it is checked.

Tip: If you are using Firebug, you can you use console.log to print in the console any element. This will display all the properties and methods possessed by that element at that point in time. This will reveal to you stuff like checked pretty quickly.

Additionally, Mootools is split into two components, Mootools core and Mootools more. The toggle method is only defined on Elements if you have Element.Shortcuts included from Mootools more or if you have written that method yourself. Otherwise you will get an error.

掀纱窥君容 2024-10-01 07:52:51

尝试 document.getElement("fieldset.toplined.toggle_slide").toggle();

try document.getElement("fieldset.toplined.toggle_slide").toggle();

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