2013-07-28 47 views
1

我有一個jquery-ui按鈕,我知道如何用css更改狀態之間的樣式,但我想知道如何更改標籤文本。例如,如下所示的ON/OFF按鈕 - 當[checked =「checked」]時,標籤文本將=「ON」(未選中時爲OFF)。用jquery ui按鈕切換文本(標籤)

<input type="checkbox" id="device_4_state" name="device_4_state" class="toggle-button on-off" checked="checked" /> 
<label for="device_4_state">ON</label> 

任何幫助將是巨大的。謝謝。

+0

你的答案就在這裏http://stackoverflow.com/questions/13652835/ button-text-toggle-in-jquery –

回答

3

這裏是解決方案:

$('#device_4_state').change(function(){ 
    if ($("#device_4_state").is(':checked')) 
    { 
     $("label[for='device_4_state']").text("ON"); 
    } 
    else 
    { 
     $("label[for='device_4_state']").text("OFF"); 
    } 

}); 
+0

非常感謝你......像一個魅力一樣工作。 –

1

如果您希望使用純CSS來設置標籤文本,您可以使用CSS僞元素來自動生成標籤的基於jQuery開發的狀態中的內容-ui按鈕。使用這種方法,離開標籤空(<label for="device_4_state"></label>)和包括CSS是這樣的:

.on-off + label > span:before { 
    content: "OFF"; 
} 
.on-off + label.ui-state-active > span:before { 
    content: "ON"; 
} 

在本的jsfiddle:http://jsfiddle.net/GjPXZ/1/