我有一個jQuery代碼片段,其中如果輸入了特定表中的任何輸入,它將更改該表中其餘輸入的類,但是如果用戶填入輸入字段,然後刪除他們的輸入它仍然改變類,但它不應該。這是我的嘗試。在jQuery中添加和刪除類
$('#lostTable :input').change(function(){
var $lostTableInput = $('#lostTable :input:not(input[type=hidden])');
var hasData = false;
$lostTableInput.each(function(){
if($(this).val().length > 0){
hasDataLf = true;
return false;
}
});
$lostTableInput.toggleClass('R', hasDataLf);
// this above part works for changing the class, and below is my attempt at changing
// it back if the user deletes the data that was entered in.
$lostTableInput.each(function(){
if($(this).val().length == 0) {
$lostTableInput.removeClass('R');
}
}
)
}
);
你聲明變量爲「hasData」,但你實際上使用了「hasDataLf」......這應該是什麼? – Pointy 2011-05-19 14:03:21
所以當任何''變化時,你會發現所有的輸入,並且如果它們中的任何一個具有非空值,則將類「R」添加到它們中的全部。然後,您想要從任何空白的課程中刪除「R」。似乎你可以做到這一點更簡單... – Pointy 2011-05-19 14:05:38
我改變了我的答案 – Neal 2011-05-19 14:08:30