简体   繁体   English

如何将选定的单选按钮移到左侧

[英]How to move the selected radio button to left side

What i am trying to achieve is that when someone clicks a radio button the button moves to the left by hiding other radio button thereby creating a foldable like effect. 我要达到的目的是,当某人单击单选按钮时,该按钮通过隐藏其他单选按钮而向左移动,从而产生可折叠的效果。

I tried this with css float:left property and its moving but can see a rectangle only. 我用css float:left属性及其移动进行了尝试,但只能看到一个矩形。 Here is my code 这是我的代码

Html HTML

<form>
  <group class="inline-radio">
    <div>
      <input id="opt1" type="radio" name="title">
      <label>opt1</label>
    </div>
    <div>
      <input id="opt2" type="radio" name="title">
      <label>opt2</label>
    </div>
    <div>
      <input id="opt3" type="radio" name="title">
      <label>opt3</label>
    </div>
    <div>
      <input id="opt4" type="radio" name="title">
      <label>opt4</label>
    </div>
    <div>
      <input id="opt5" type="radio" name="title">
      <label>others</label>
    </div>
  </group>

</form>

javascript javascript

//for toggling  
var hid = false;

$("group.inline-radio").click(function() {
  if (hid == false) {

    $('group.inline-radio').find('input[type="radio"]').not(':checked').hide().siblings('label').hide();

    hid = true;
    return;
  } else {
    $('group.inline-radio').find('input[type="radio"]').not(':checked').show().siblings('label').show();

    hid = false;
  }

});

Here is the code in codepen http://codepen.io/flyingboy007/pen/ojoVWe 这是Codepen中的代码http://codepen.io/flyingboy007/pen/ojoVWe

Change the div selector in your css to this: 将CSS中的div选择器更改为此:

div {
position: relative;
width: 20%;
float:left;
}

And change your javascript to this: 并将您的JavaScript更改为此:

var hid = false;

$("group.inline-radio").click(function() {
  if (hid == false) {

    $('group.inline-radio').find('input[type="radio"]').not(':checked').parent().hide();

    hid = true;
    return;
  } else {
     $('group.inline-radio').find('input[type="radio"]').not(':checked').parent().show();

     hid = false;
   }

});

Codepen: http://codepen.io/anon/pen/ojpNVq Codepen: http ://codepen.io/anon/pen/ojpNVq

You need to hide the div wrapping the unchecked radio elements. 您需要隐藏包含未选中的单选元素的div。 Then you don't have to worry about the labels because they are inside the parent div as well. 然后,您不必担心标签,因为它们也位于父div中。

http://codepen.io/anon/pen/LpeYod http://codepen.io/anon/pen/LpeYod

var hid = false;

$("group.inline-radio").click(function() {
  if (hid == false) {

    $('group.inline-radio').find('input[type="radio"]').not(':checked').parent().hide();

    hid = true;
    return;
  } else {
    $('group.inline-radio').find('input[type="radio"]').not(':checked').parent().show();

    hid = false;
  }

});

Also, unless you want the selected div to fill the entire element then you need set specific widths on them. 另外,除非您希望所选的div填充整个元素,否则您需要在其上设置特定的宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM