PHP JQuery Checkbox - If checkbox is pre-checked, it wont remove 'checked' attribute when unchecked
Having trouble with checkboxes that are being pre-checked (when fetching record from mysql). So I am trying to create a way for users to edit a record in the database. One of the items on the form is a checkbox. I check to see if the record being edited has this feature, if it does I check the box. When I attempt to uncheck it, then use jquery/ajax to submit the form and update, it still sees the box as checked. Using firebug, I noticed that when I uncheck and check the box, it never removes the attribute checked='checked'. Any ideas??
Here is the checkbox code
<input type="checkbox" id="account_ad_city" value="1" <?PHP if($row['hascityad']==1)echo "checked=\"checked\"";?>>
JQuer开发者_如何转开发y/AJAX value setting of checkbox before passing to mysql update page:
if($('#account_ad_city').attr('checked'))
var ad_city = $('#account_ad_city').val();
Use
$('#account_ad_city').prop('checked'));
Try it here - http://jsfiddle.net/FloydPink/mvdzS/
And this answer here on SO itself gives a good explanation on the difference between .attr()
and .prop()
精彩评论