你好,我有这个错误:
未捕获的TypeError:无法读取未定义的属性'id'
在编辑和保存1条记录时,它可以正确执行,但是当我尝试编辑另一条记录时,它向我显示错误。
我尝试了各种解决方案,并阅读了有关reload()的信息;但这并没有解决错误。你可以帮帮我吗。谢谢
这是代码
显示要编辑的数据
<form action="" class="form-horizontal" method="POST">
<input type="hidden" id="id_reg" name="id_reg" value="">
<input type="hidden" id="opcion" name="opcion" value="registrar">
<input type="text" name="identification" id="identification" disabled>
<input type="text" name="nombre" id="nombre" disabled>
<input type="text" id="apellido" name="apellido" disabled>
<input type="text" id="estado" name="estado" autofocus>
<input type="text" id="fecha" name="fecha" autofocus>
<input type="text" id="hora" name="hora" autofocus>
<input type="submit" value="Guardar">
<input type="button" id="btn_listar" value="listar">
</form>
<table id="assistance" class="table table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Cedula</th>
<th>Nombre</th>
<th>Apellido</th>
<th>Estado</th>
<th>Fecha</th>
<th>Hora</th>
<th>id user</th>
<th></th>
</tr>
</thead>
</table>
$(document).ready( function () {
listar();
guardar();
});
保存数据
var guardar = function(){
$("form").on("submit", function(e){
e.preventDefault();
var frm = $(this).serialize();
$.ajax({
method: "POST",
url: "guardar.php",
data: frm
}).done( function( info ){
console.log( info );
var json_info = JSON.parse( info );
mostrar_mensaje(json_info);
limpiar_datos();
listar();
});
});
}
获取列表数据
var listar = function(){
var table = $("#assistance").DataTable({
"destroy":true,
"ajax":{
"method":"POST",
"url":"listar.php"
},
"columns":[
{"data":"id"},
{"data":"identification"},
{"data":"first_name"},
{"data":"last_name"},
{"data":"estado"},
{"data":"assistance_date"},
{"data":"assistance_time"},
{"data":"id_subscriber"},
{"defaultContent":"<button type='button' class='editar btn btn-primary'>Edit</button> <button type='button' class='eliminar btn btn-danger' data-toggle='modal' data-target='#modalEliminar' >Delete</i></button>"}
],
"language":idioma_espanol
});
/* Call functions get data for edit and reload */
obtener_data_editar("#assistance tbody", table);
setInterval( function (tbody, table) {
$(table).DataTable({ajax: "data.json"}).ajax.reload(null, false);
});
}
获取数据进行编辑
var obtener_data_editar = function(tbody, table){
$(tbody).on("click", "button.editar", function(){
var data = table.row( $(this).parents("tr") ).data();
console.log(table.row( $(this).parents("tr") ).data());
var id = $("#id_reg").val( data.id ),
id_subs = $("#id_subs").val(data.id_subs),
identification = $("#identification").val(data.identification),
first_name = $("#nombre").val(data.first_name),
last_name = $("#apellido").val(data.last_name),
estado= $("#estado").val(data.estado),
assistance_date= $("#fecha").val(data.assistance_date),
assistance_time= $("#hora").val(data.assistance_time),
opcion = $("#opcion").val("modificar");
});
}