2012-06-21 127 views
0

this非常相似,但在我的情況下,標籤的值可能會更改。我試圖在點擊時改變字體。這裏是我迄今:用於複選框的動態標籤的Jquery選擇器

// Job Type Button 
    $('fieldset.workexperience input').on('change', function() { 
     if ($(this).hasClass('jobtype') && ($(this).attr("name")!="")) { 
      $('.'+$(this).attr("name")).toggle("slow"); 
      //Change color to white to show hidden 
       $('label[for=$(this)]').toggleClass("hidden"); 

       $(this).toggleClass("hidden"); 

       var $this = $(this).attr("label"); 
       $('label[$this]').toggleClass("hidden"); 
     } else { 

     } 
    }); 

有三次嘗試,首先由其標籤訪問它,二來直接訪問它(不過$(這)是複選框,而不是標籤),其次是嘗試獲取標籤的值,然後將其作爲參數傳遞。

任何幫助將不勝感激。

BTW:CSS是:

.hidden { 
    color: white; 
    } 

回答

2
$('label[for=$(this)]').toggleClass("hidden"); 

應該

$('label[for="'+ $(this).attr('id') +'"]').toggleClass("hidden"); 

例如:

<label for="somecheckbox">Checkbox</label> 
<input type="checkbox" id="somecheckbox"> 

所以$(this).attr('id')將得到id,這等於labelfor屬性。

+0

OH!,所以你可以結束引號和連接,然後重新打開引號,類似於連接字符串? –

+0

@chrisFrisina yup .... – thecodeparadox