添加一行,并用AJAX提交数据。
function submitForm() {
var name = $("#name").val();
var description = $("#description").val();
var url = $("#url").val();
$.ajax({
url: '/admin/ops',
type: 'post',
data: {admin_site: {"name": name, "description": description, "url": url}},
success: function(data) {
if(data.success){
var tb = document.getElementById("admin_site_table")
var row = tb.insertRow(tb.rows.length-1);
var cell = row.insertCell();
cell.innerHTML = "<a href=" + url + ">" + name + "</a>";
cell = row.insertCell();
cell.innerText = description;
cell = row.insertCell();
cell.innerHTML = "<a href=" + url + ">" + url + "</a>";
cell = row.insertCell();
cell.innerHTML = "<input type='button' value='删除' class='btn btn-danger' onclick = 'delRow(this, " + data.id + ")'/>";
$("#name").val("");
$("#description").val("");
$("#url").val("");
}else{
alert("添加失败!");
}
}
});
}
删除一行,并用AJAX实现提交数据。
function validate() {
if (confirm("确定删除?")) {
return true;
} else {
return false;
}
}
function delRow(btn, admin_site_id) {
if(validate()){
$.ajax({
url: '/admin/ops/' + admin_site_id,
type: 'delete',
success: function(data) {
if(data.success){
var row = btn.parentNode.parentNode;
row.parentNode.removeChild(row);
}else{
alert("删除失败!");
}
}
});
}
}