php jquery error in Firefox
I'm having trouble with some Javascript, and hopefully someone may be able to help! First, here is the code:
function Client_Selected() {
var temp = $("input:radio[name='selected_client']:checked").val();
var f_input_name = "fname_" + temp;
var l_input_name = "lname_" + temp;
var first = $("input[name='"+f_input_name+"']").val();
var last = $("input[name='"+l_input_name+"']").val();
var content = first + " " + last;
var thispage = $("input[name='current_page']").val();
// Now Load the Notes!!!
dataString = 'web_id='+temp+'&Section='+thispage;
$.ajax({
type: "POST",
url: "<?=URL_SSL_WEBSITE_ROOT?>/notes/loadClientNotesAJAX",
data: dataString,
success: function(msg) {
$("#notes_body").html(msg);
$("#note_client_name").html(content);
}
});
$("#button_client_name").html(content);
$("#note_client_name").html(content);
$("input:radio[name=selected_client]:checked").attr(checked, true);
};
My problem is with the last line. What's going on, is I have a list of clients who are associated with a radio button, when a button is clicked I make an AJAX call to get/display information about the client. If the last line is not present, then when I select a second client, it loads the information but the new client's radio button does not stay checked and instead the previous (or first) selected clients radio button remains checked.
With the last line I get an error of: "checked is undefined"
despite the error everything worked and the appropriate checkbox remained checked. However, with the new update to firefox yesterday ... the appropriate checkbox remains checked, though my success function never runs on the ajax call, and the javascript simply stops on the last line. It still works in IE, but I need to get this working again in firefox. Any Ideas?
I've tried changing it to: $("input:radio[name=selected_client]:checked").attr('checked', true); Which then the success function runs, and the information is populated on my webpage, however I then get the problem of the newly selected check开发者_C百科box NOT remaining checked and the previous one remains checked.
Any Help is much appreciated.
Thanks
.attr('checked','checked');
will check your checbox/radio box and .removeAttr('checked');
to uncheck it.
Looks like some quotes are missing
$("input:radio[name=selected_client]:checked").attr('checked', 'checked');
try the last line in the success function of the ajax call.
and use attr('checked',true)
checked is undefined
It certainly is. You used checked
; JavaScript is looking for a variable called checked
, which doesn't exist. You meant to say 'checked'
with quotes, as a string.
However,
$("input:radio[name=selected_client]:checked").attr('checked', true);
Does nothing because you are checking a radio which by definition from your selector is already checked. What are you trying to do, here?
Note that if you are doing this in a radio button onclick
handler, the :checked
radio during the execution of the function will not be the newly-clicked one, it'll be the one that was previously checked. It is not until onclick
event handling is finished that the click-to-change-checkedness actually goes through. If you want to find out the new radio during an onclick
, you will have to look at this
or the event
object, depending on how you're calling the function.
Re other answers:
attr('checked','checked')
Can we please stop giving this out? It works, but not because of the reasons you think. jQuery attr()
does not set HTML attributes (except in a couple of corner cases trying to work around browser bugs). It sets JavaScript properties, but trying to fix up the names to match the HTML attributes. So:
attr('checked', true);
or false
for unsetting, is the correct approach. The value 'checked'
only works because it is truthy when evaluated for a boolean property. Any non-empty string would do exactly the same.
If jQuery did set HTML attributes, attr('checked')
would not work, because the HTML checked
attribute corresponds to the initial setting of the checkbox (aka defaultChecked
), not the current checkedness state.
精彩评论