你好我使用jquery ui toggleswitch toggleswitch.js我可以获得价值,但
如何在javascript或jquery中以编程方式设置开/关值
我在更改时使用此代码,但我想将其设置为文档就绪
$('.toggleEmailStatus').toggleSwitch().change(function (ev) {
alert(ev);
try {
console.log("value= " + $(this).val());
} catch (e) {
alert(e);
}
});
答案 0 :(得分:0)
您可以通过triggering点击相应的复选框以编程方式打开或关闭它:
jQuery(document).ready(function($) {
$('.toggleswitch').toggleSwitch();
$("#opt2").trigger("click"); // turn it on
});
<link href="https://raw.githack.com/jamiebicknell/Toggle-Switch/master/toggleswitch.css" rel="stylesheet" />
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js'></script>
<script type='text/javascript' src='https://raw.githack.com/jamiebicknell/Toggle-Switch/master/jquery.toggleswitch.js'></script>
<label for='opt1'>Option 1</label>
<input type='checkbox' name='opt1' id='opt1' value='1' class='toggleswitch' checked='checked' />
<!-- Switch is OFF -->
<label for='opt2'>Option 2 (off by default)</label>
<input type='checkbox' name='opt2' id='opt2' value='1' class='toggleswitch' />