2016-02-09 24 views
0

我的HTML代碼需要在選擇單選按鈕時更改(父母)的類別。 jQuery的

<td valign="top" class="padtop10 appgreyunselect"> 
    <input name="raddate" type="radio" value="trans-date" class="resmarpad rad" /> Transaction Date 
</td> 
<td valign="top" class="padtop10 appgreyunselect"> 
    <input name="raddate" type="radio" value="collect-date" class="resmarpad rad" /> Collection Date 
</td> 

我的js代碼

$("#myForm input").click(function(){ 
    if($('input[name=raddate]:checked')){ 
     $(this).parent().removeClass('appgreyunselect').addClass('fontwht11'); 
    } else { 
     $(this).parent().removeClass('fontwht11').addClass('appgreyunselect'); 
    } 
}); 

實際寫入正在做完美的東西的代碼,但是這不是我想要的, 如果未選擇則標籤會回到以前的類(appgreyunselect)。但他們仍然是白色的。 請幫忙...

+0

將'.click'替換爲'.change'。另外,你也可以添加CSS,甚至更好,創建一個片段或bin。 –

回答

0

關於更改單選按鈕刪除fontwht11類和添加appgreyunselect.padtop10。如果選中單選按鈕,則刪除appgreyunselect類,並將fontwht11添加到父級,如下所示。

$("#myForm input").change(function() { 
    $('.padtop10').removeClass('fontwht11').addClass('appgreyunselect'); 

    if ($(this).is(':checked')) { 
     $(this).parent().removeClass('appgreyunselect').addClass('fontwht11'); 
    } 
}); 
+0

謝謝阿齊姆,它工作。非常感謝。 –

相關問題