2014-12-05 63 views
0

嗨即時通訊使用jQuery UI的toggleswitch toggleswitch.js我可以關上的價值,但jQuery的撥動開關programmatcally上關閉

我如何能在JavaScript或jQuery的

設置開/關值編程我用這在變化的代碼,但我想將其放置在文件準備

$('.toggleEmailStatus').toggleSwitch().change(function (ev) { 
     alert(ev); 
     try { 

      console.log("value= " + $(this).val()); 

     } catch (e) { 
      alert(e); 
     }  
}); 

回答

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' />