Several related questions already exist on this site and elsewhere, but none of the answers seems to apply to my situation. I have a bunch of radio buttons in HTML.
<input type="radio" name="b1" value="aa"> aa<br>
<input type="radio" name="b1" value="ab"> ab<br>
<input type="radio" name="b1" value="ac"> ac<br>
<input type="radio" name="b2" value="ba"> ba<br>
<input type="radio" name="b2" value="bb"> bb<br>
<input type="radio" name="b2" value="bc"> bc<br>
<script type="text/javascript">
var PK = {
modes: ["b1", "b2"],
init: function() {
// attach actions to radio buttons
for (var key in this.modes) {
var mode = this.modes[key];
$("input[name='" + mode + "']").bind(
"change",
function() {
PK[mode]($("input[name='" + mode + "']:checked").val());
}
);
}
},
b1: function(val) { .. do something .. },
b2: function(val) { .. do something else .. },
};
$(document).ready(function() {
PK.init();
});
</script>
The above doesn't work at all. I have tried .live instead of .bind, and even that doesn't work as expected. In fact, both of them bind the action to whatever is the last entry in modes, in the above, "b2". That is, the function PK.b2() is fired whether I change the value in b1 or b2 buttons. If I reverse the modes entries, the last one takes on the value. How can I accomplish he above successfully?