2012-07-04 115 views
1

我有一個HTML元素(使用MVC3剃刀)JQuery的:當複選框被點擊

 <h2 class="price"> 
       @Html.LabelFor(vm => vm.Price) 
       @Html.Encode(@Model.Price) 
     </h2> 
     <div id="checkbox-price"> 
       @Html.CheckBoxFor(vm => vm.IncludeToPayment) Include in payment. 
     </div> 

我怎樣才能改變h2元素的樣式,(在這種情況下,我想改變文本的元素的風格裝飾:通過裏面的項目)當用戶取消選中/複選框使用JQUERY?

在此先感謝。

回答

4
$('#checkbox-price input:checkbox').change(function(){ 
    if (!$(this).is(':checked')) { 
    $('.price').css('text-decoration', 'line-through') 
    } else { 
    $('.price').css('text-decoration', 'none') 
    } 
}) 

$('#checkbox-price input:checkbox').change(function() { 
      // or $(`input[type="checkbox"]') 
      var $obj = $('.price'); 
      if (!$(this).is(':checked')) { 
       $obj.css('text-decoration', 'line-through'); //or addClass     
      } else { 
       $obj.css('text-decoration', 'none'); // or addClass     
      } 
     }) 
+1

這麼快! :) 會嘗試! – lincx

+0

如果我有很多複選框,怎麼樣?這是否也觸發,如果我檢查其他複選框?對不起,我是一個jQuery初學者 – lincx

+0

@csharplinc在這種情況下應該修改代碼,這取決於標記的結構。 – undefined

2

下面是一個簡單的代碼和live demo

這適用於所有的複選框

$('input[type="checkbox"]').click(function() { 
    if (this.checked) { 
     //alert('Checked'); 
     $("h2").addClass('newclass'); 
    } else { 
     //alert('Unchecked'); 
     $("h2").removeClass('newclass'); 
    } 
});​ 

如果你想爲特定的複選框使用下面的申請

$('input[type="checkbox"]') to $('input.someclass) 

+1

++ 1給你! –

+0

+1用於類別切換 – lincx