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):
How is possible to select a value, preferebly with jQuery?
答案 0 :(得分:1)
JQM文档在这里有些令人困惑。
flipswitch
有两种:
select
元素之上构建的拨动开关checkbox
元素之上构建的拨动开关(不知道为什么还有一个slider
拨动开关)
select
或checkbox
,方法与您完全相同带有标准HTML非移动元素:如果您需要通过PHP构建网页,则可以使用selected
或checked
属性来预先选择一个选项/值。
以下是代码初始化的示例:
$(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>
/* Maybe forgotten in JQM standard CSS */
a.ui-flipswitch-on {
color: inherit !important;
}