下面是 jQuery 代码,它在 ajax 请求成功时动态创建包含一些行的表。
$(document).ready(function () {
$("#rule_search_submit").click(function () {
var search_rule_name = $("#search_rule_name").val();
//alert(search_rule_name);
$.ajax({
url: "searchrule",
type: "POST",
data: "search_rule_name=" + search_rule_name,
success: function (data) {
$.each(data, function (k, item) {
$("#search_rule_table").after('<tr id="table_row_id"><td><input type="radio" name="rule_radio" id="rule_radio" value="' + item.rule_name + ',' + item.rule_desc + ',' + item.start_date + ',' + item.end_date + ',' + item.rule_id + ',' + item.status + '"/></td><td>' + item.rule_name + '</td></tr>');
});
},
error: showCustError
});
});
});
它使用单选按钮和标题创建两行。以下是单击单选按钮时从数据库访问值的代码
$(document).ready(function () {
$("input[name='rule_radio']").live("change", function () {
var parent = $(this).parent().parent();
var id_name = parent.attr("id");
var rule_data = $(this).val().split(",");
document.getElementById("edit_rule_name").value = rule_data[0];
document.getElementById("edit_rule_desc").value = rule_data[1];
document.getElementById("edit_start_date").value = rule_data[2];
document.getElementById("edit_end_date").value = rule_data[3];
document.getElementById("edit_ruleid").value = rule_data[4];
});
});
下面是在某些 ajax 响应上替换动态创建的表行的代码
$(document).ready(function () {
$("#save_rule").click(function () {
var ruleid = document.getElementById("edit_ruleid").value;
var rulename = document.getElementById("edit_rule_name").value;
var ruledesc = document.getElementById("edit_rule_desc").value;
var start_date = document.getElementById("edit_start_date").value;
var end_date = document.getElementById("edit_end_date").value;
var type = "editrule";
if (ruleid == "")
alert('Please select the rule to edit');
else {
$.ajax({
url: "addrule",
type: "GET",
data: "ruleid=" + ruleid + "&rulename=" + rulename + "&ruledesc=" + ruledesc + "&start_date=" + start_date + "&end_date=" + end_date + "&type=" + type,
success: function (data) {
$("#table_row_id").replaceWith('<tr id="table_row_id"><td><input type="radio" name="rule_radio" id="rule_radio" value="' + data[0].rule_name + ',' + data[0].rule_desc + ',' + data[0].start_date + ',' + data[0].end_dat e + ',' + data[0].rule_id + ',' + data[0].status + '"/></td><td>' + data[0].rule_name + '</td> </tr>');
},
error: showCustError
});
}
});
});
不幸的是,我无法在 ajax 响应中动态替换行。请建议我解决此任务。