How to programmatically select jQuery slider?

时间:2019-03-19 15:01:46

标签: jquery jquery-mobile

I am trying to preselect a slider in jQuery mobile.

<select name="box" id="box" data-role="slider" data-mini="true">
    <option value="off" selected></option>
    <option value="on" >mit</option>
</select>

This somehow does not work: $("#box").val("on").flipswitch( "refresh" ) ;

Therefore I created the box via PHP and preselect:

<select name="box" id="box" data-role="slider" data-mini="true">
        <?php
        $box['on'] = 'mit'; 
        $box['off'] = '';   
        foreach ($box as $key => $value) {
                ($key == $_GET['box']) ? $isselected = ' selected=""' : $isselected = false;
            echo '<option value="'.$key.'" '.$isselected .'>'.$value.'</option>';
        } 
    ?>
</select>

Resulting in:

<select name="box" id="box" data-role="slider" data-mini="true">
    <option value="on"  selected="">mit</option>
    <option value="off" ></option>
</select>

But in this case the switch looks like this (missing th blue):

enter image description here

How is possible to select a value, preferebly with jQuery?

1 个答案:

答案 0 :(得分:1)

JQM文档在这里有些令人困惑。 flipswitch有两种:

  1. select元素之上构建的拨动开关
  2. checkbox元素之上构建的拨动开关

(不知道为什么还有一个slider拨动开关)

两者都像移动式 flippswitches 一样,您可以根据自己的需要自由使用selectcheckbox,方法与您完全相同带有标准HTML非移动元素:如果您需要通过PHP构建网页,则可以使用selectedchecked属性来预先选择一个选项/值。

以下是代码初始化的示例:

$(document).on("flipswitchcreate", "#flip-checkbox", function(e) {
  $(this).prop("checked", true).flipswitch("refresh");
});

$(document).on("flipswitchcreate", "#flip-select", function(e) {
  $(this).val("on").flipswitch("refresh");
});

$(document).on("slidecreate", "#flip-slider", function(e) {
  $(this).val("on").slider("refresh");
});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">
  <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
  <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
</head>
<body>
  <div id="page-one" data-role="page">
    <div role="main" class="ui-content">
      <label for="flip-checkbox">Flip toggle switch checkbox:</label>
      <input type="checkbox" data-role="flipswitch" id="flip-checkbox" name="flip-checkbox">
      <label for="flip-select">Flip toggle switch select:</label>
      <select data-role="flipswitch" id="flip-select" name="flip-select">
        <option value="off" selected="">Off</option>
        <option value="on">On</option>
      </select>
      <label for="flip-slider">Flip toggle switch slider:</label>
      <select data-role="slider" id="flip-slider" name="flip-slider">
        <option selected="" value="off">Off</option>
        <option value="on">On</option>
      </select>
    </div>
  </div>
</body>
</html>


关于 active 状态,您是对的。我也注意到了,所以我默认在CSS中使用它:

/* Maybe forgotten in JQM standard CSS */
a.ui-flipswitch-on {
    color: inherit !important;
}