One, data of the HTML attribute data property to embed custom data data - * = ""
There is a problem in the project, often need to get the current row or value of a position of, for example, I need to project a foreach loop output row <tr> php file in the background, and then the <tr> reached the front page,
foreach ($res as $k => $v) {
$str .= "<tr>
<td>{$v['0']}</td>
<td><input value='{$v['1']}'></td>
<td><input value='{$v['2']}'></td>
<td><input value='{$v['3']}'></td>
<td><input value='{$v['4']}'></td>
<td>
<button id=\"btn\" data-bumen=\"{$v['2']}\" onclick='dele(this)' value=\"{$v['0']}\" >立即删除</button>
<button onclick='sub(this)' value=\"{$v['0']}\" >立即修改</button>
</td>
</tr>";
}
In this case $ v I is a single row in the database, [ '], to obtain the value of the current field by $ v, the first button has a button set attribute data, which is stored in it when $ v row per cycle [ '2'] value,
This value is not the same every time, and reception is determined in js
function dele(parm) {
var id = parm.value;
var type = 'del';
var btn = document.getElementById("btn");
var btn_vue = btn.dataset.bumen;
// console.log(btn_vue);
var xml = new XMLHttpRequest;
xml.onreadystatechange = function() {
if (xml.readyState == 4 && xml.status == 200) {
alert(xml.responseText);
if(btn_vue == " Finance Department " ) {
document.getElementById ( ' BMA ' ) .click ();
}
IF (btn_vue == " Production unit " ) {
document.getElementById ( ' BMB ' ) .click ();
}
IF (btn_vue == " investment portion " ) {
document.getElementById ( ' BMC ' ) .click ();
}
}
}
xml.open("POST", "/select.php");
xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xml.send('id=' + id + '&type=' + type);
}
var btn obtained button, btn_vue declare variables to obtain the value of the custom property (i.e. attribute data obtained $ [ '2'])
var btn = document.getElementById("btn");
was btn_vue = btn.dataset.bumen;
<button data-bumen=\"{$v['2']}\"> 立即删除 </button>;
此时我们已经获得这个值,但是有一点之前已经说过,$v[' 2 '] 这个值不是固定的,所以我们在ajax接收的时候要进行判断
if (btn_vue == "财务部") {
document.getElementById('bma').click();
}
if (btn_vue == "生产部") {
document.getElementById('bmb').click();
}
if (btn_vue == "招商部") {
document.getElementById('bmc').click();
}
通过判断不同的值,来创建一个虚拟点击操作,实现自动刷新的一个功能