4

$("#check1")以下のステートメントで2回目の使用を回避する方法はありますか?

$("#check1").prop("checked",!$("#check1").prop("checked"));

どんなトリックでも私は以下のようなことをすることができますか?

$("#check1").prop("checked",!this.prop("checked"));
4

4 に答える 4

5

関数をコールバックとして渡すと、現在の値が取得されます。

$("#check1").prop("checked", function(i, value) {
    return !value;
});

.prop( propertyName, function(index, oldPropertyValue) )

  • propertyNameThe name of the property to set.
  • function(index, oldPropertyValue) A function returning the value to set. Receives the index position of the element in the set and the old property value as arguments. Within the function, the keyword this refers to the current element.

参照

于 2012-12-28T18:48:54.380 に答える
3
var el = $("#check1");

el.prop("checked",!el.prop("checked"));
于 2012-12-28T18:46:50.967 に答える
1
var $check = $("#check1");
$check.prop( "checked", !$check.prop("checked") );
于 2012-12-28T18:46:21.773 に答える
-2

あなたはこれを使うことができます、この方法で:

$("#check1").prop("checked",!$(this).prop("checked"));
于 2012-12-28T18:48:20.737 に答える