感谢您抽出宝贵时间回答我的问题。
我有以下代码标记+ jquery代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.css" />
<script src="http://code.jquery.com/jquery-1.7.js" type="text/javascript"></script>
<script src="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div data-role="page" id="home" class="type-home">
<div data-role="content">
<div class="content-primary">
<p>
The flip toggle switch is displayed like this:</p>
<div data-role="fieldcontain">
<label for="slider">
Flip switch:</label>
<select name="slider" id="my-slider" data-role="slider">
<option id="firstOption" data-test="" data-status="deactivated" value="off">Activated</option>
<option id="secondOption" data-test="" data-status="activated" value="on">Deactivated</option>
</select>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
<script type="text/javascript">
$(document).ready(function () {
changeOption();
$('#my-slider').bind('change', function () {
var myswitch = $(this);
if (myswitch.val() == 'on') {
alert('on');
}
else {
alert('off');
}
});
});
function changeOption() {
var x = 1;
if (x == 1) {
$('#firstOption').attr('selected', 'selected').slider('refresh');
}
else {
$('#secondOption').attr('selected', 'selected').slider('refresh');
}
}
</script>
当我点击翻转开关时,它会执行jquery代码(两个警报)。但是,我希望能够根据Web服务提供的值设置翻盖开关的默认值(我在本例中使用虚拟值=&gt; x)。但是,我无法选择第二个元素......它总是默认为“已激活”。有什么建议?非常感谢你!