2016-04-14 59 views
0

我想獲取切換複選框的值。 我的意思是,我想讓我說'1'準備好,'0'準備好就說出來。 我該怎麼做? 這是我的代碼:如何獲取切換複選框的值

<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet"> 

<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script> 
<input type="checkbox" checked data-toggle="toggle" data-on="Ready" data-off="Not Ready" data-onstyle="success" data-offstyle="danger"> 

回答

0

首先,您將ID設置爲複選框。然後聽取更改。

<input id="my_checkbox" type="checkbox" onclick="getValue()"... 

function getValue() { 
    var isChecked = document.getElementById('my_checkbox').checked; 
    var the_value = isChecked ? 1 : 0; 
    //do something with that value 
} 

this JsFiddle

+0

我做到了,但遺憾的是沒有結果。 data-onstyle =「未就緒」data-onstyle =「成功」data-offstyle =「危險的」data-on =「準備就緒」data-off =「未準備好」 (); '

+0

'document.ready'是jquery風格。你有它包括在你的HTML? –

+0

加'$(this).val()'不會做任何事情。如果你想去jquery樣式,寫'alert($(this).is(「:checked」)); ' –