简体   繁体   English

全选/取消全选jQuery Mobile中的所有选项

[英]Select All / DeSelect All Options in jQuery Mobile

I'm trying to do a select all, deselect all with jQuery Mobile. 我试图做一个全选,用jQuery Mobile取消全选。 It seems trivial, but been pulling my hear out all day about this. 看起来微不足道,但是整天都在不听我的话。 This is what it looks like: 看起来是这样的:

Here's the JSFiddle . 这是JSFiddle

在此处输入图片说明

<div data-role="page" id="one">
  <div data-role="content">
    <label for="sel">Select the Options</label>
    <select name="sel" id="sel" data-native-menu="false" multiple="multiple">
      <option value="-1" selected>-All Brands-</option>
      <option value="1">Prod 1</option>
      <option value="2">Prod 2</option>
      <option value="3">Prod 3</option>
    </select>
  </div>
</div>

JavaScript: JavaScript的:

var arr = [];
$("#sel").live("change", function (event, ui) {
    var txt = $("#sel option:selected").text();
    if (txt.search("-All Brands-")) {

        $("#sel option").attr("selected", "selected");

    }
    else {
        $("#sel option:selected").removeAttr("selected");

    }
    $("#sel").selectmenu("refresh", true);
})

I've tried a bunch of stuff and can't get it just right. 我尝试了很多东西,但并不能完全正确。

Ok the jquery mobile multi select only has a change event, as you probably read. 好的,您可能已经读过,jQuery mobile多重选择仅具有更改事件。 So the only way I can think of is to store the state of the all brands item and then check if it's changed, and select or deselect all. 因此,我唯一想到的方法是存储所有品牌项目的状态,然后检查其是否已更改,然后选择或取消选择所有项目。 Here's your JSFiddle with the changes: 这是您的JSFiddle所做的更改:

http://jsfiddle.net/Gm89P/7/ http://jsfiddle.net/Gm89P/7/

like this: 像这样:

var arr = [];
$("#sel").on("change", function (event, ui) {

    var allSelected = $("option:first", this).attr("selected");

    if (this.wasAllSelected && !allSelected) {
        $("#sel option:selected").removeAttr("selected");
        this.wasAllSelected = allSelected;
    }
    else if (!this.wasAllSelected && allSelected) {
        $("#sel option").attr("selected", "selected");
        this.wasAllSelected = allSelected;
    }
    $("#sel").selectmenu("refresh", true);
})

If you choose to manipulate the classes you can do it manually like this: 如果选择操作这些类,则可以这样手动进行:

// For de-selecting

$( '#yourSelectMenuId'  ).val( '' ).change();
$( '#yourSelectMenuId-listbox ul li a' ).removeClass( 'ui-checkbox-on' );
$( '#yourSelectMenuId-listbox ul li a' ).addClass( 'ui-checkbox-off' );

don't forget adding -listbox extension to your select menu id which jqm added automatically. 不要忘记将-listbox扩展名添加到jqm自动添加的选择菜单ID中。

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

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