如果在加载时选中复选框,则将类添加到 DIV

发布于 2024-11-03 15:22:42 字数 942 浏览 2 评论 0原文

我需要有关脚本的帮助,以在选中隐藏复选框时将“活动”类添加到 div。这一切都发生在一个有点复杂的形式中,可以保存并稍后编辑。过程如下:

我有一系列隐藏的复选框,当单击可见的 DIV 时会选中这些复选框。感谢一些人,特别是之前帖子中的 Dimitar Christoff,我有一些简单的脚本可以处理所有事情:

  1. 一个人点击一个 div:

    yadda yadda

  2. 一个活动类添加到 div 中:

     $$('.thumb').addEvent('click', function(){
        this.toggleClass('tactive');
      });
    
  3. 相应的复选框被选中:

    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:

  1. A person clicks on a div:

    <div class="thumb left prodata" data-id="7"> yadda yadda </div>

  2. An active class is added to the div:

      $('.thumb').addEvent('click', function(){
        this.toggleClass('tactive');
      });
    
  3. 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 技术交流群。

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

发布评论

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

评论(2

十六岁半 2024-11-10 15:22:42
window.addEvents({
  load: function(){
    if (checkbox.checked){
      document.getElements('.thumb').fireEvent('click');
    }
  }
});

示例: http://jsfiddle.net/vCH9n/

window.addEvents({
  load: function(){
    if (checkbox.checked){
      document.getElements('.thumb').fireEvent('click');
    }
  }
});

Example: http://jsfiddle.net/vCH9n/

云柯 2024-11-10 15:22:42

好吧,如果有人感兴趣的话,这是最终的解决方案。其作用是:为 DIV 类创建一个单击事件以切换活动类 onclick,并且还使用 = 复选框 ID 的 data-id="X" 将每个 DIV 与复选框相关联。最后,如果重新加载表单(在这种情况下,可以保存表单并稍后进行编辑),最后一段 javascript 就会看到在页面加载时选中了哪些复选框,并触发 DIV 的活动类。

要查看所有操作,请在此处查看:https:// /www.worklabs.ca/2/add-new/add-new?itemetype=website (脚本当前正在第三个选项卡“选择样式”上运行)。除非您是会员,否则您将无法保存/编辑它,但它可以工作:)您可以使用 firebug 取消隐藏复选框并自己切换复选框以查看。

window.addEvent('domready', function() {

// apply the psuedo event to some elements
$('.thumb').addEvent('click', function() {
    this.toggleClass('tactive');
});

$('.cbox').addEvent('click', function() {
   var checkboxes= $('.cbox');

       for(i=1; i<=checkboxes.length; i++){
               if(checkboxes[i-1].checked){
               if($('c_'+checkboxes[i-1].id))
                       $('c_'+checkboxes[i-1].id).set("class", "thumb tactive");
               }
       else{
               if($('c_'+checkboxes[i-1].id))
                       $('c_'+checkboxes[i-1].id).set("class", "thumb");
               }
       }
});

// Add the active class to the corresponding div when a checkbox is checked onLoad... basic idea:
var checkboxes= $('.cbox');

for(i=1; i<=checkboxes.length; i++){
    if(checkboxes[i-1].checked){
       $('c_field_tmp_'+i).set("class", "thumb tactive");
    }
}

document.getElements("div.thumb").addEvents({
      click: function(e) {
        if (e.target.get("tag") != 'input') {
          var checkbox = document.id("field_tmp_" + this.get("data-id"));
          checkbox.set("checked", !checkbox.get("checked"));
        }
      }
    });

  });

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.

window.addEvent('domready', function() {

// apply the psuedo event to some elements
$('.thumb').addEvent('click', function() {
    this.toggleClass('tactive');
});

$('.cbox').addEvent('click', function() {
   var checkboxes= $('.cbox');

       for(i=1; i<=checkboxes.length; i++){
               if(checkboxes[i-1].checked){
               if($('c_'+checkboxes[i-1].id))
                       $('c_'+checkboxes[i-1].id).set("class", "thumb tactive");
               }
       else{
               if($('c_'+checkboxes[i-1].id))
                       $('c_'+checkboxes[i-1].id).set("class", "thumb");
               }
       }
});

// Add the active class to the corresponding div when a checkbox is checked onLoad... basic idea:
var checkboxes= $('.cbox');

for(i=1; i<=checkboxes.length; i++){
    if(checkboxes[i-1].checked){
       $('c_field_tmp_'+i).set("class", "thumb tactive");
    }
}

document.getElements("div.thumb").addEvents({
      click: function(e) {
        if (e.target.get("tag") != 'input') {
          var checkbox = document.id("field_tmp_" + this.get("data-id"));
          checkbox.set("checked", !checkbox.get("checked"));
        }
      }
    });

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