I have radio buttons in HTML like this:
1
-
The best way to set radiobuttons state in jquery:
HTML:
Orange
Pink
Black
Pinkish Purple
Jquery (1.4+) code to pre-select one button :
var presetValue = "black";
$("[name=color]").filter("[value='"+presetValue+"']").attr("checked","checked");
In Jquery 1.6+ code the .prop() method is preferred :
var presetValue = "black";
$("[name=color]").filter("[value='"+presetValue+"']").prop("checked",true);
To unselect the buttons :
$("[name=color]").removeAttr("checked");
- 热议问题