チェックボックスの値を配列に追加/削除するためのより良い方法があるかどうか疑問に思っています。indexOf()
残念ながら、IE8 / 7をサポートする必要があるため、ECMAScript5を使用できません。
実用的なjsfiddleの例を次に示します。http://jsfiddle.net/puyQr/
このメソッドの機能は次のとおりです。
/**
* If a user clicks onto the checkbox, the value of the value attribute is
* only added to the array when the value not yet exists. However, if
* the value already exists, the value is removed.
*/
var values = [];
jQuery(':checkbox').on('click', function(){
var index = jQuery.inArray(this.value, values);
if(index === -1) {
values.push(this.value);
} else {
values.splice(index, 1);
}
});