簡體   English   中英

如果檢查了輸入無線電,​​則更改父級的顏色:無法使其與 jQuery 一起使用

[英]if an input radio is checked change the parent's color: can't make it work with jQuery

當我單擊input單選時, <label>的文本應更改其文本顏色。

但是,我的 jQuery 代碼並不能解決問題:

$("#radios").find("input").change(function(){
if ($(this).checked)
    $(this).parent().addClass('libelulas');
else
    $(this).parent().removeClass('libelulas');
});

我一直在檢查 chrome 中的檢查員,似乎它甚至沒有創建 class 無論是父級(標簽)還是輸入。

這是我的 jsfiddle http://jsfiddle.net/Arkl1te/2MnEU/4/

您需要將其包裝在$(document).ready中並在change()事件中使用正確的選擇器:

$(document).ready(function(){
  $(".radios input[type=radio]").change(function(){
      $(this).parent().addClass('libelulas');
  });
});

jsFiddle在這里。


更新:

忘記在問題中給出與您的代碼相關的示例,而不僅僅是 jsFiddle:

$(document).ready(function(){ 
  $(".radios input[type=radio]").change(function(){
      $(this).parent().addClass('libelulas');
      $("input[type=radio]:not(:checked)").parent().removeClass('libelulas');
  }); 
});

jsFiddle在這里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM