JQuery: setting radio button in group based on if
This SHOULD b开发者_运维技巧e simple, I just know it should.
Basically, I have the code below in the error function of an $.ajax call. The call itself works. The call happens when the value of a radiobutton group goes from true to false or vice versa. If the error gets hit, I want to reset it to the previous value. What have I got wrong?
if ($("input[name='enableEmail']:checked").val() == "true") {
$("input[name='enableEmail'][value='false']").attr("checked", true);
}
else {
$("input[name='enableEmail'][value='true']").attr("checked", true);
}
EDIT Apparently the question wasn't clear enough. I have two radio buttons in a group: On and Off. If user toggles from Off to On, a $.ajax call happens. If an error happens in the $.ajax call, I want to set the radio button back to Off. If the user toggles On to Off, I want it to go back to On in the event of an error
First check if you are using jQuery UI and need to call .buttonset("refresh"). :-)
HTML and other contextual code would be a big help. Lacking that, have you tried using firebug or something along those lines to do some sanity checks on values and jQuery selectors by outputting to the console?
if ($("input[name='enableEmail']:checked").val() == "true") {
console.log("Got here 1: " + $("input[name='enableEmail'][value='false']").attr("checked"));
$("input[name='enableEmail'][value='false']").attr("checked", true);
}
else {
console.log("Got here 2: " + $("input[name='enableEmail'][value='true']").attr("checked"));
$("input[name='enableEmail'][value='true']").attr("checked", true);
}
Error for ajax is called when the ajax requests fails. Are you sure such an error occurs and not some error in logic that returns undesired results ?
Seems to work for me ... http://jsfiddle.net/gaby/zGvRR/
I assume you have runat="server"
on the input tags for the radio buttons. Remove runat
tag and try. it should work.
.attr("checked", "checked");
Try that. It's not a boolean value.
精彩评论