var rowcount = 0;
function addrow() {
rowcount++;
console.log("nis" + rowcount);
if (rowcount >= 2) {
return false;
rowcount = 0;
}
document.getElementById("myTableData").style.display = "block";
var el = document.createElement('input');
el.type = 'text';
el.name = 'kname' + rowcount;
var hid = document.createElement('input');
hid.type = 'hidden';
hid.name = 'hid';
hid.value = rowcount;
var del = document.createElement('input');
del.type = 'button';
del.name = 'delll';
del.value = '';
del.style.width = '30px';
del.style.height = '26px';
del.style.border = 'none';
del.style.outline = 'none';
del.style.background = 'url(./images/del-hover.png) no-repeat';
del.onclick = function () {
tr.parentElement.removeChild(tr);
rowcount--;
if (rowcount == 0 || rowcount >= 2) {
document.getElementById("myTableData").style.display = "none";
rowcount = 0;
}
};
var el_r = document.createElement('input');
el_r.type = 'radio';
el_r.name = 'kgender' + rowcount;
el_r.value = 'female';
el_r.defaultChecked = true;
var el_r2 = document.createElement('input');
el_r2.type = 'radio';
el_r2.name = 'kgender' + rowcount;
el_r2.value = 'male';
var obj1 = document.createTextNode("Female");
var obj2 = document.createTextNode("Male");
var objLabel = document.createElement("label");
objLabel.htmlFor = el_r.id;
objLabel.appendChild(el_r);
objLabel.appendChild(obj1);
var objLabel2 = document.createElement("label");
objLabel2.htmlFor = el_r2.id;
objLabel2.appendChild(el_r2);
objLabel2.appendChild(obj2);
var el_s = document.createElement('select');
el_s.name = "day1" + rowcount;
el_s.onchange = function () {
value_d = el_s.options[el_s.selectedIndex].value;
};
for (var i = 1; i < 32; i++) {
var j = i;
j = document.createElement('option');
j.text = i;
j.value = i;
el_s.appendChild(j);
}
var month = new Array("January", "Februray", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var el_sm = document.createElement('select');
el_sm.name = "month1" + rowcount;
el_sm.onchange = function () {
var valuem = el_sm.options[el_sm.selectedIndex].value;
};
for (var i = 0; i < month.length; i++) {
var j = i;
j = document.createElement('option');
j.text = month[i];
j.value = i + 1;
el_sm.appendChild(j);
}
var el_sy = document.createElement('select');
el_sy.name = "year1" + rowcount;
el_sy.onchange = function () {
var valuey = el_sy.options[el_sy.selectedIndex].value;
};
for (var i = 2013; i > 1950; i--) {
var j = i;
j = document.createElement('option');
j.text = i;
j.value = i;
el_sy.appendChild(j);
}
var table = document.getElementById("myTableData");
var tableBody = document.createElement('TBODY');
table.appendChild(tableBody);
var tr = document.createElement('TR');
tableBody.appendChild(tr);
var td = document.createElement('TD');
td.width = '175';
td.appendChild(el);
tr.appendChild(td);
var td = document.createElement('TD');
td.width = '245';
td.appendChild(objLabel);
td.appendChild(objLabel2);
tr.appendChild(td);
var td = document.createElement('TD');
td.width = '245';
td.appendChild(el_s);
td.appendChild(el_sm);
td.appendChild(el_sy);
tr.appendChild(td);
var td = document.createElement('TD');
td.width = '20';
td.appendChild(del);
tr.appendChild(td);
var td = document.createElement('TD');
td.width = '10';
td.appendChild(hid);
tr.appendChild(td);
myTableData.appendChild(table);
}
我的HTML
<td colspan="4">
<div id="myTableData" style="display:none;">
<table>
<tr>
<td width="175">NAME</td>
<td width="245"> Gender</td>
<td width="245">Date of Birth</td>
</tr>
</table>
</div>
</td>
<td colspan="2">
<div class="addkidbg" onClick="addrow()" />ADDKID</div>
</td>
当我点击addrow按钮时它向我显示三行,但我需要用户只点击一次,如果用户删除行和列名称应该消失但是如果我点击addrow按钮两次然后点击删除按钮.......它显示列名称。如果用户删除该行并且不能多次点击
,我希望整个部分应该消失答案 0 :(得分:0)
function addrow() {
rowcount++;
console.log("nis" + rowcount);
if (rowcount >= 2) {
rowcount = 0;
return false;
}
}
<div class="addkidbg" onClick="return addrow();" />ADDKID</div>
试试这段代码。您的函数返回一个值,因此首先在onclick事件
上写入返回值