【问题标题】:Embedding radio buttons嵌入单选按钮
【发布时间】:2015-08-21 12:20:27
【问题描述】:

我想在更多单选按钮中嵌入单选按钮,例如:https://jsfiddle.net/xa6ow1jq/

小提琴的行为与我想要的完全一样,但是对于 2x3 网格来说似乎有很多代码,我计划至少有一个 3xN 网格(每层有 3 层 N 个按钮,N 是至少 10 个,但如果用户继续滚动,则更多)......所以我想知道是否有人知道/可以想到更有效的方法来做到这一点。 (使用 php 和/或 javascript 和/或 jquery 和/或 jquery UI)

(我是 javascript 和 jquery 菜鸟,目前(自)从昨天开始学习它,所以如果您能对技术术语保持温和并尽可能多地解释,我将不胜感激。

提前致谢。

小提琴中的javascript代码:

// main buttons
$(document).ready(function(){
    $(".ap").click(function(){
        $(".a").toggle();
        $(".b").hide();
        $(".c").hide();
        $(".a.l").hide();
    });
});

$(document).ready(function(){
    $(".bp").click(function(){
        $(".a").hide();
        $(".b").toggle();
        $(".c").hide();
        $(".b.l").hide();
    });
});

$(document).ready(function(){
    $(".cp").click(function(){
        $(".a").hide();
        $(".b").hide();
        $(".c").toggle();
        $(".c.l").hide();
    });
});

//secondary buttons
//a
$(document).ready(function(){
    $(".a1").click(function(){
        $(".a1.l").toggle();
        $(".a2.l").hide();
        $(".a3.l").hide();
    });
});
$(document).ready(function(){
    $(".a2").click(function(){
        $(".a1.l").hide();
        $(".a2.l").toggle();
        $(".a3.l").hide();
    });
});
$(document).ready(function(){
    $(".a3").click(function(){
        $(".a1.l").hide();
        $(".a2.l").hide();
        $(".a3.l").toggle();
    });
});
//b
$(document).ready(function(){
    $(".b1").click(function(){
        $(".b1.l").toggle();
        $(".b2.l").hide();
        $(".b3.l").hide();
    });
});
$(document).ready(function(){
    $(".b2").click(function(){
        $(".b1.l").hide();
        $(".b2.l").toggle();
        $(".b3.l").hide();
    });
});
$(document).ready(function(){
    $(".b3").click(function(){
        $(".b1.l").hide();
        $(".b2.l").hide();
        $(".b3.l").toggle();
    });
});
//c
$(document).ready(function(){
    $(".c1").click(function(){
        $(".c1.l").toggle();
        $(".c2.l").hide();
        $(".c3.l").hide();
    });
});
$(document).ready(function(){
    $(".c2").click(function(){
        $(".c1.l").hide();
        $(".c2.l").toggle();
        $(".c3.l").hide();
    });
});
$(document).ready(function(){
    $(".c3").click(function(){
        $(".c1.l").hide();
        $(".c2.l").hide();
        $(".c3.l").toggle();
    });
});

【问题讨论】:

  • 1.使用一次$(document).ready() 事件。 2. 如果您的代码中有重复的功能 - 请考虑编写一个函数并调用它,而不是一遍又一遍地编写相同的代码。

标签: javascript php jquery twitter-bootstrap jquery-ui


【解决方案1】:

在下面形成你的html,这样你就可以处理3xN行,你需要弹出到javascript中显示为// here的数组,并相应地实现你的HTML,

$(document).ready(function() {

  var groups = ['a', 'b', 'c'];
    // creating simple js array too use for DOM manipulation

  $.each(groups, function(k, id) {
      // loops groups array we just created id variable contains a, b and then c
    $('#' + id).hide();
      // will evaluate as $('#a').hide();
    $('#' + id + 'l').hide();
      // will evaluate as $('#al').hide();
  });

  $(".button").click(function() {
    // bind click event on DOM items having class name as 'button'

    var button_id = $(this).data('id'); 
      /* $(this) will get us which button has been clicked, every 
      time click event occurs on DOM items having button class 
      and $(this).data(id); will get us clicked button's data-id attribute */

    $('#' + button_id).toggle(); // toogle

    var hide = $.grep(groups, function(value) { 
      // http://api.jquery.com/jquery.grep/
      return value != button_id;
    });

    $.each(hide, function(k, id) {
      // http://api.jquery.com/each/
      $('#' + id).hide();
    });

  });

  var selector = []; // initialize blank array

  $.each(groups, function(k) {
    selector.push('.' + groups[k]); 
      /* push groups array's elements with an extra .
      so, .a .b and .c */

  });

    // join array elements with ,
  selector = selector.join(',');
    // now selector is string, having value .a,.b,.c
    // https://api.jquery.com/category/selectors/

  $(selector).click(function() {
    // binding an event to the string we just created, follow the link above to get more idea

    var button_id = $(this).data('id'); // clicked button's data-id attribute
    var class_id = $(this).attr('class'); // clicked button's class

    var flag = $('.' + class_id + 'l').filter('[data-id="' + button_id + '"]').is(':visible');
      /* for later use, will be true if elements with matched filter conditions is visible in DOM,
      false otherwise */

    $.each(groups, function(k, id) {
      $('#' + id + 'l').children().hide();
        // https://api.jquery.com/children/
    });

    $.each(groups, function(k, id) {
      $('#' + id + 'l').hide();
    });

    $('#' + class_id + 'l').show();

    if (flag)
      $('.' + class_id + 'l').filter('[data-id="' + button_id + '"]').hide();
    else
      $('.' + class_id + 'l').filter('[data-id="' + button_id + '"]').show();
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="button" data-id="a">Toggle a</button>
<button class="button" data-id="b">Toggle b</button>
<button class="button" data-id="c">Toggle c</button>

<div id="a">
  <div class="a" data-id="1"><button>Toggle a1</button></div>
  <div class="a" data-id="2"><button>Toggle a2</button></div>
  <div class="a" data-id="3"><button>Toggle a3</button></div>
</div>

<div id="b">
  <div class="b" data-id="1"><button>Toggle b1</button></div>
  <div class="b" data-id="2"><button>Toggle b2</button></div>
  <div class="b" data-id="3"><button>Toggle b3</button></div>
</div>

<div id="c">
  <div class="c" data-id="1"><button>Toggle c1</button></div>
  <div class="c" data-id="2"><button>Toggle c2</button></div>
  <div class="c" data-id="3"><button>Toggle c3</button></div>
</div>

<div id="al">
  <div class="al" data-id="1">this is line a1</div>
  <div class="al" data-id="2">this is line a2</div>
  <div class="al" data-id="3">this is line a3</div>
</div>

<div id="bl">
  <div class="bl" data-id="1">this is line b1</div>
  <div class="bl" data-id="2">this is line b2</div>
  <div class="bl" data-id="3">this is line b3</div>
</div>

<div id="cl">
  <div class="cl" data-id="1">this is line c1</div>
  <div class="cl" data-id="2">this is line c2</div>
  <div class="cl" data-id="3">this is line c3</div>
</div>

【讨论】:

  • 谢谢。它几乎可以满足我的需要,除了一件事:例如,如果我单击按钮 A,然后单击按钮 A1,然后单击按钮 B(或再次单击按钮 A),则“这是 A1 行”div 不会消失。我目前正在尝试了解您的 javascript 代码,以便我可以解决此问题,并且因为我想了解它以便在 javascript 方面变得更好,但这对我来说真的很复杂。如果您能花点时间修复它和/或评论 JS 代码以帮助我理解每一行的作用,我将不胜感激。
  • 所以,通过添加这两行代码,我能够理解部分代码并修复它:15 : $('#' + button_id + 'l').hide();23 : $('#' + id +'l').hide();。我仍然不明白28 : var selector = []; 行之后的内容,如果您能评论代码以帮助我理解它,我仍然会很感激!无论如何,非常感谢您的帮助。
  • 此外,如果我是对的,这适用于 2xN 按钮,但它仍然需要适应 3xN 按钮,对吧?
  • 给你,伙计,看我编辑的答案,我还添加了这个代码来改进codereview,如果你想看看,click here。是的,我把那个错误留给你作为练习:),我无法理解 2xN 的含义,如果你能给我看一个 4xN 的小提琴,我能理解。
猜你喜欢
  • 2017-02-22
  • 2017-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多