2011-10-25 57 views
0

假設我在表單中有四個複選框Check-a,Check-b,Check-c,Check-d。Jquery跟蹤複選框已從已選中更改爲未選中或已被取消選中

Check-a和Check-b已被選中。我可以通過使用Jquery來獲得哪個複選框被選中或未選中。

現在我沒有選中Check-a。所以Check-a被檢查並且我已經通過檢查來取消選中。因此,現在Check-c和Check-d已經被取消選中,Check-a沒有被我選中。我正在尋找一種方法來跟蹤哪些未被選中,哪些未被選中。

任何人都可以告訴我一個方法嗎?

+0

當複選框是'.change()'d時,只需設置一個布爾變量。 – Blazemonger

+0

你能幫我一個示例code.Will我必須維護一個布爾數組呢? –

+0

我無法幫助您使用示例代碼,直到看到您的代碼。你如何存儲布爾變量取決於你想要對它們做什麼。 – Blazemonger

回答

1

假設相同的HTML作爲@definitelyundefinable,這樣做那種同樣的事情,但因爲它不使用隱藏域,而假陣列(如隱藏字段字符串)

這是稍微乾淨
$(function() { 
    var history = []; 
    $("input").click(function() { 
    history.push({ 
     id: this.id, 
     checked: this.checked 
    }); 
    }); 
    // If you'd like, you can initialize the array 
    // with the initial values with the following line 
    $("input").click(); 

}); 
1

如果你需要一種點擊歷史,爲什麼不只是使用額外的領域?

HTML:

<form> 
    <div> 
     <input type="checkbox" name="fruit" value="o" id="orange" /> 
     <label for="orange">orange</label> 
    </div> 
    <div> 
     <input type="checkbox" name="fruit" value="a" id="apple" /> 
     <label for="apple">apple</label> 
    </div> 
    <div> 
     <input type="text" name="history" id="history" /> 
    </div> 
</form> 

jQuery腳本:

$("input").click(function() { 
    $("#history").val($("#history").val() + $(this).val() + ";"); 
}); 

你可以把它隱藏和評估;分離之後列表..

+0

有沒有必要使它成爲一個文本字段,使用一個真實的數組 –

+0

真實,但如果它必須發送到服務器無論如何,Id猜對了一個字符串可能是一個簡單的解決方案 –

相關問題