2012-07-04 70 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