Can't Check Dynamic HTML CheckBox
Via ajax I'm building up a dynamic html rows to a table and appending the rows via jquery. I add a checkbox to a cell in开发者_JAVA技巧 my row and return the row. Problem is no check appears when you click the checkbox. I can see the checkbox. I stuck in an alert to see the value of text returned. Do I have to add some kind of OnClick event just to get the check to appear?
$(table).append(row);
value of row
<table>
<tr>
<td><input type="checkbox" name="vehicle1" value="Bike" />
</td>
</tr>
</table>
Edit ajax code:
function BindRows(id) {
$.ajax({
type: "POST",
url: "Default.aspx/GetTable",
data: "{'id':'" + id + "'}",
contentType: "application/json",
dataType: "json",
success: function(data) {
var name = data.d[0];
var status = data.d[1];
var row = data.d[2];
var table = '#' + name + '_table_status' + status;
$('#<%= lblConfirmMsg.ClientID %>').html(row);
if (status.length > 0) {
$(table + ' tr').remove();
$(table).append(row);
alert(row);
}
},
error: function(request, error) {
$('#<%= lblConfirmMsg.ClientID %>').html("ERROR: " + error);
}
})
return false;
}
Just had some similar problem (buttons/checkboxes did nothing) because I've called event.preventDefault() in a not so good place. Maybe this was your case too ;)
Do you have onClick event handler set for either checkbox, td or tr? If so, you shouldn't return false from it as it will prevent checkbox from being checked.
This happens when a parent HTML Element has an onClick Attribute which does not return true.
Make sure that all parent onClick Attributes ends like this:
onClick="...your code...; return true;"
and your are fine.
after append the row
$('input[type=checkbox]').live('click', function() {
var o = $(this);
o.append("checked='yes'");
});
精彩评论