radio(单选框)

radio用于单选的情况

DOM结构

  1. <div class="mui-input-row mui-radio">
  2. <label>radio</label>
  3. <input name="radio1" type="radio">
  4. </div>

默认radio在右侧显示,若希望在左侧显示,只需增加.mui-left类即可,如下:

  1. <div class="mui-input-row mui-radio mui-left">
  2. <label>radio</label>
  3. <input name="radio1" type="radio">
  4. </div>

若要禁用radio,只需在radio上增加disabled属性即可;

mui基于列表控件,提供了列表式单选实现;在列表根节点上增加.mui-table-view-radio类即可,若要默认选中某项,只需要在对应li节点上增加.mui-selected类即可,dom结构如下:

  1. <ul class="mui-table-view mui-table-view-radio">
  2. <li class="mui-table-view-cell">
  3. <a class="mui-navigate-right">Item 1</a>
  4. </li>
  5. <li class="mui-table-view-cell mui-selected">
  6. <a class="mui-navigate-right">Item 2</a>
  7. </li>
  8. <li class="mui-table-view-cell">
  9. <a class="mui-navigate-right">Item 3</a>
  10. </li>
  11. </ul>

列表式单选在切换选中项时会触发selected事件,在事件参数(e.detail.el)中可以获得当前选中的dom节点,如下代码打印当前选中项的innerHTML:

  1. var list = document.querySelector('.mui-table-view.mui-table-view-radio');
  2. list.addEventListener('selected',function(e){
  3. console.log("当前选中的为:"+e.detail.el.innerText);
  4. });

扩展阅读

代码块激活字符:

mradio