如何使用jquery mobile制作可折叠复选框?

时间:2012-06-29 20:07:12

标签: jquery-mobile checkbox collapsable

我想对某些复选框进行分组,并使其可折叠,以及选择/取消全部。

HTML

<div style="padding-left: 5px; padding-right: 5px">
 <fieldset data-role="controlgroup"> 
  <input autocomplete="off" type="checkbox" name="layers" id="land" class="layers" checked="checked"/>
  <label for="land">Land Parcels</label> 
  <input autocomplete="off" type="checkbox" name="layers" id="road" class="layers" checked="checked"/>
  <label for="road">Roads</label> 
  <input autocomplete="off" type="checkbox" name="layers" id="rail"  class="layers" checked="checked"/>
  <label for="rail">Railroads</label> 
input autocomplete="off" type="checkbox" name="layers" id="lake"  class="layers" checked="checked"/>
  <label for="lake">Lakes</label>
  <input autocomplete="off" type="checkbox" name="layers" id="points" class="layers"/>
  <label for="points">AMIS Points</label>
  <input autocomplete="off" type="checkbox" name="education" id="education" class="layers"/>
  <label for="education">Education</label>
    <input autocomplete="off" type="checkbox" data-mini="true" name="education" id="childcare" class="layers"/>
    <label for="childcare">Child Care</label>
    <input autocomplete="off" type="checkbox" data-mini="true" name="education" id="highschool" class="layers"/>
    <label for="highschool">High School</label>
    <input autocomplete="off" type="checkbox" name="layers" id="stormpipes" class="layers"/>
  <label for="stormpipes">Storm Pipes</label>
 </fieldset>
</div>

JQuery的

$(document).ready(function () {
    fixContentHeight();
    $('.layers').click(changeLayers);
});

function changeLayers () {
    var checked = [];
    $('.layers').each(function () {
        if ($(this).attr('checked')) {
            checked.push($(this).attr('value'));
        }
    });
    mainLayer.params.LAYERS = checked.join(",");
    mainLayer.redraw();
}

$('#education').click(function() {
    $("INPUT[type='checkbox']").attr('checked', $('#education').is(':checked'));
});

在教育复选框内尝试让托儿所和高中学校成为可能。

1 个答案:

答案 0 :(得分:0)

您可以使用data-role="listview"的无序列表,将data-role="collapsible"添加到li元素,并在内部列表的li

<ul data-role="listview">
    <li data-role="collapsible">
        <h3>Title of collapsible row</h3>
        <ul data-role="listview">
            <li>
                <input autocomplete="off" type="checkbox" name="layers" id="land" class="layers" checked="checked"/>
                <label for="land">Land Parcels</label> 
            </li>
            <li>
                <input autocomplete="off" type="checkbox" name="layers" id="road" class="layers" checked="checked"/>
                <label for="road">Roads</label> 
            </li>
        </ul>
    </li>
</ul>

边距和填充看起来有点奇怪,但你可以调整css来解决这个问题