如果在加载时选中复选框,则将类添加到 DIV
我需要有关脚本的帮助,以在选中隐藏复选框时将“活动”类添加到 div。这一切都发生在一个有点复杂的形式中,可以保存并稍后编辑。过程如下:
我有一系列隐藏的复选框,当单击可见的 DIV 时会选中这些复选框。感谢一些人,特别是之前帖子中的 Dimitar Christoff,我有一些简单的脚本可以处理所有事情:
一个人点击一个 div:
yadda yadda一个活动类添加到 div 中:
$$('.thumb').addEvent('click', function(){ this.toggleClass('tactive'); });
相应的复选框被选中:
document.getElements("a.add_app").addEvents({ 单击:函数(e){ if (e.target.get("tag") != '输入') { var checkbox = document.id("field_select_p" + this.get("data-id")); checkbox.set("已选中", !checkbox.get("已选中")); } }
});
现在,我需要第四个(也是最后一个)函数来完成该项目(使用 mootools 或只是简单的 javascript,没有 jQuery)。当表单保存后加载时,我需要一种方法将活动类添加回相应的 div。基本上颠倒了这个过程。我正在尝试自己解决这个问题,并且很乐意发表一个想法,但我尝试过的任何事情都不好。我想我至少会在我处理这个问题时发布这个问题。提前致谢!
I need help with a script to add an "active" class to a div when a hidden checkbox is checked. This all happening within a somewhat complex form that can be saved and later edited. Here's the process:
I have a series of hidden checkboxes that are checked when a visible DIV is clicked. Thanks to a few people, especially Dimitar Christoff from previous posts here, I have a few simple scripts that handle everything:
A person clicks on a div:
<div class="thumb left prodata" data-id="7">
yadda yadda</div>
An active class is added to the div:
$('.thumb').addEvent('click', function(){ this.toggleClass('tactive'); });
The corresponding checkbox is checked:
document.getElements("a.add_app").addEvents({ click: function(e) { if (e.target.get("tag") != 'input') { var checkbox = document.id("field_select_p" + this.get("data-id")); checkbox.set("checked", !checkbox.get("checked")); } }
});
Now, I need a fourth ( and final ) function to complete the project (using mootools or just plain javascript, no jQuery). When the form is loaded after being saved, I need a way to add the active class back to the corresponding div. Basically reverse the process. I AM trying to figure it out myself, and would love to post an idea but anything I've tried is, well, bad. I thought I'd at least get this question posted while I work on it. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
示例: http://jsfiddle.net/vCH9n/
Example: http://jsfiddle.net/vCH9n/
好吧,如果有人感兴趣的话,这是最终的解决方案。其作用是:为 DIV 类创建一个单击事件以切换活动类 onclick,并且还使用 = 复选框 ID 的 data-id="X" 将每个 DIV 与复选框相关联。最后,如果重新加载表单(在这种情况下,可以保存表单并稍后进行编辑),最后一段 javascript 就会看到在页面加载时选中了哪些复选框,并触发 DIV 的活动类。
要查看所有操作,请在此处查看:https:// /www.worklabs.ca/2/add-new/add-new?itemetype=website (脚本当前正在第三个选项卡“选择样式”上运行)。除非您是会员,否则您将无法保存/编辑它,但它可以工作:)您可以使用 firebug 取消隐藏复选框并自己切换复选框以查看。
Okay, in case anyone is interested, here is the final solution. What this does is: Create a click event for a DIV class to toggle an active class onclick, and also correlates each DIV to a checkbox using a data-id="X" that = the checkbox ID. Finally, if the form is reloaded ( in this case the form can be saved and edited later ) the final piece of javascript then sees what checkboxes are checked on page load and triggers the active class for the DIV.
To see it all in action, check it out here: https://www.worklabs.ca/2/add-new/add-new?itemetype=website ( script is currently working on the third tab, CHOOSE STYLE ). You won't be able to save/edit it unless you're a member however, but it works:) You can unhide the checkboxes using firebug and toggle the checkboxes yourself to see.