Javascript 在jquery中使用键存储数据
单击行的“编辑”按钮或“删除”按钮时,我希望存储学生id以及选中的类型,即编辑或删除类型,然后使用它更新数据库中的数据。目前,我已经编写了html和jquery,以便在按下相应按钮时选中复选框。现在我陷入了如何将数据存储在数组中并将其转发的困境。数据的格式应类似于stdList(id=1,check='edit') 这是我的htmlJavascript 在jquery中使用键存储数据,javascript,jquery,html,Javascript,Jquery,Html,单击行的“编辑”按钮或“删除”按钮时,我希望存储学生id以及选中的类型,即编辑或删除类型,然后使用它更新数据库中的数据。目前,我已经编写了html和jquery,以便在按下相应按钮时选中复选框。现在我陷入了如何将数据存储在数组中并将其转发的困境。数据的格式应类似于stdList(id=1,check='edit') 这是我的html <div class="alert alert-danger" role="alert"></div> <table class="t
<div class="alert alert-danger" role="alert"></div>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Checklist</th>
<th>Id</th>
<th>Student Name</th>
<th>Address</th>
<th>Phone</th>
<th>Class</th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" id="editCheck" class="editCheck" />
<input type="checkbox" id="deleteCheck" class="deleteCheck" />
</td>
<td>1</td>
<td>
<input type="text" class="form-control item" readonly="readonly" />
</td>
<td>
<input type="text" class="form-control item" readonly="readonly" />
</td>
<td>
<input type="text" class="form-control item" readonly="readonly" />
</td>
<td>12</td>
<td>
<button type="button" class="btn btn-info btn-xs" id="btn1">Edit</button>
</td>
<td>
<button type="button" class="btn btn-danger btn-xs" id="dbtn1">Delete</button>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="editCheck" class="btn2" />
<input type="checkbox" id="deleteCheck" />
</td>
<td>1</td>
<td>
<input type="text" class="form-control item" readonly="readonly" />
</td>
<td>
<input type="text" class="form-control item" readonly="readonly" />
</td>
<td>
<input type="text" class="form-control item" readonly="readonly" />
</td>
<td>12</td>
<td>
<button type="button" class="btn btn-info btn-xs" id="btn2">Edit</button>
</td>
<td>
<button type="button" class="btn btn-danger btn-xs" id="dbtn2">Delete</button>
</td>
</tr>
</tbody>
清单
身份证件
学名
地址
电话
等级
行动
1.
12
编辑
删除
1.
12
编辑
删除
这是jquery
$('.btn.btn-info.btn-xs').click(function () {
var $tr = $(this).closest('tr')
$tr.find('input:checkbox').first().prop('checked', 'true');
$tr.find('input').removeAttr('readonly');
check($tr);
});
$('.btn.btn-danger.btn-xs').click(function () {
var $tr = $(this).closest('tr');
$tr.find('input:checkbox').last().prop('checked', 'true');
check($tr);
});
function check($tr) {
if (($tr.find('input:checkbox').first().is(":checked")) && ($tr.find('input:checkbox').last().is(":checked"))) {
$('.alert.alert-danger').append('<p>You can not edit and update the field at once</p>');
}
}
$('.btn.btn info.btn xs')。单击(函数(){
var$tr=$(this).closest('tr')
$tr.find('input:checkbox').first().prop('checked','true');
$tr.find('input').removeAttr('readonly');
支票($tr);
});
$('.btn.btn danger.btn xs')。单击(函数(){
var$tr=$(this.closest('tr');
$tr.find('input:checkbox').last().prop('checked','true');
支票($tr);
});
功能检查($tr){
如果($tr.find('input:checkbox').first()是(“:checked”)&($tr.find('input:checkbox').last()是(“:checked”)){
$('.alert.alert danger')。追加('您不能立即编辑和更新该字段);
}
}
根据上述问题,您需要保存学生详细信息和调用的操作。因此,下面的方法也可以用来做同样的事情
首先,可以在表行
tr
元素的数据属性中设置id。还向可在数组/对象中使用的复选框添加值
<tr data-id="1">
<td>
<input type="checkbox" id="editCheck" class="editCheck" value="edit" />
<input type="checkbox" id="deleteCheck" class="deleteCheck" value="delete" />
...
<tr data-id="1">
<td>
<input type="checkbox" id="editCheck" class="editCheck" value="edit" />
<input type="checkbox" id="deleteCheck" class="deleteCheck" value="delete" />
...
function check($tr) {
var $checked = $tr.find('input:checked');
var data = {};
if ($checked.length > 1) {
$('.alert.alert-danger').append('<p>You can not edit and update the field at once</p>');
return false;
}
data.id = $tr.data('id');
data.check = $checked.val();
console.log(data); // Gives something like {id: 1, check: 'edit'}
}