垂直显示的jquery按钮组

nol*_*ogo 16 jquery-ui

我正在使用jquery ui按钮,是否可以垂直显示单选按钮?它似乎默认为水平.

http://jqueryui.com/demos/button/#radio

非常感谢

ede*_*ohe 31

我写了一个简短的插件,用于实现单选按钮的垂直按钮和jquery ui的复选框.

1. jQuery插件

(function( $ ){
//plugin buttonset vertical
$.fn.buttonsetv = function() {
  $(':radio, :checkbox', this).wrap('<div style="margin: 1px"/>');
  $(this).buttonset();
  $('label:first', this).removeClass('ui-corner-left').addClass('ui-corner-top');
  $('label:last', this).removeClass('ui-corner-right').addClass('ui-corner-bottom');
  var maxWidth = 0; // max witdh
  $('label', this).each(function(index){
     var labelWidth = $(this).width();
     if (labelWidth > maxWidth ) maxWidth = labelWidth ; 
  })
  $('label', this).each(function(index){
    $(this).width(maxWidth);
  })
};
})( jQuery );
Run Code Online (Sandbox Code Playgroud)

2.样本标记

<h2> Radio Buttonset </h2>
<div id="radio">
  <input type="radio" id="radio1" name="radio" value="1"/><label for="radio1">Choice 1</label>
  <input type="radio" id="radio2" name="radio" value="2"/><label for="radio2">Choice 2</label>
  <input type="radio" id="radio3" name="radio" value="3"/><label for="radio3">Choice 3</label>
</div>
<h2> Checkbox Buttonset </h2>
<div id="checkbox">
  <input type="checkbox" id="check1" name="check" value="1"/><label for="check1">Choice 1</label>
  <input type="checkbox" id="check2" name="check" value="2"/><label for="check2">Choice 2</label>
  <input type="checkbox" id="check3" name="check" value="3"/><label for="check3">Choice 3</label>
</div>
Run Code Online (Sandbox Code Playgroud)

3.插件使用

$(document).ready(function(){
    //call plugin 
    $("#radio").buttonsetv();
    $("#checkbox").buttonsetv();
});
Run Code Online (Sandbox Code Playgroud)

这里是插件和示例代码

我希望这可以帮到你:)

  • 请在下次发布代码,而不是简单的链接.链接很好,但它们可能会在以后完全关闭或删除.因此,在没有解决方案的情况下让未来的人们需要解决方案. (4认同)