如何避免在提交后创建表单元素仍然存在?
How to avoid creating form element stays even after the submission?
当我执行另一个时,我必须通过匹配 condition.But 来制作元素,之前创建的元素保持 there.How 以避免使用 java 脚本重复这种情况?
我有 Javascript 函数像这样:
function maketext(p) {
var f = document.createElement('form');
f.setAttribute('method', "post");
f.setAttribute('id', "update");
f.setAttribute('action', "updateprod");
if (p == "Code") {
var inp = document.createElement('input');
inp.setAttribute("placeholder", "Type Here");
inp.setAttribute("class", "form-control");
inp.setAttribute("id", "mcode");
inp.setAttribute("name", "mcode");
var sub = document.createElement("input");
sub.setAttribute("type", "submit");
sub.setAttribute("value", "submit");
sub.setAttribute("class", "btn btn-primary");
f.appendChild(inp);
f.appendChild(sub);
document.getElementById("mys").appendChild(f);
}
if (p == "Name") {
var inp = document.createElement('input');
inp.setAttribute("placeholder", "Type Here");
inp.setAttribute("class", "form-control");
inp.setAttribute("id", "mname");
inp.setAttribute("name", "mname");
var sub = document.createElement("input");
sub.setAttribute("type", "submit");
sub.setAttribute("value", "submit");
sub.setAttribute("class", "btn btn-primary");
f.appendChild(inp);
f.appendChild(sub);
document.getElementById("mys").appendChild(f);
}
}
Html
<a href="#" onclick="maketext('Code')" data-toggle="modal" data-target="#myModal" class="btn btn-info btn-lg">Edit</a>
<a href="#" onclick="maketext('Name')" data-toggle="modal" data-target="#myModal" class="btn btn-info btn-lg">Edit</a>
在创建元素之前从 div
中移除所有先前的元素
function maketext(p) {
document.getElementById("mys").innerHTML = ''
// rest of the code
}
当我执行另一个时,我必须通过匹配 condition.But 来制作元素,之前创建的元素保持 there.How 以避免使用 java 脚本重复这种情况?
function maketext(p) {
var f = document.createElement('form');
f.setAttribute('method', "post");
f.setAttribute('id', "update");
f.setAttribute('action', "updateprod");
if (p == "Code") {
var inp = document.createElement('input');
inp.setAttribute("placeholder", "Type Here");
inp.setAttribute("class", "form-control");
inp.setAttribute("id", "mcode");
inp.setAttribute("name", "mcode");
var sub = document.createElement("input");
sub.setAttribute("type", "submit");
sub.setAttribute("value", "submit");
sub.setAttribute("class", "btn btn-primary");
f.appendChild(inp);
f.appendChild(sub);
document.getElementById("mys").appendChild(f);
}
if (p == "Name") {
var inp = document.createElement('input');
inp.setAttribute("placeholder", "Type Here");
inp.setAttribute("class", "form-control");
inp.setAttribute("id", "mname");
inp.setAttribute("name", "mname");
var sub = document.createElement("input");
sub.setAttribute("type", "submit");
sub.setAttribute("value", "submit");
sub.setAttribute("class", "btn btn-primary");
f.appendChild(inp);
f.appendChild(sub);
document.getElementById("mys").appendChild(f);
}
}
Html
<a href="#" onclick="maketext('Code')" data-toggle="modal" data-target="#myModal" class="btn btn-info btn-lg">Edit</a>
<a href="#" onclick="maketext('Name')" data-toggle="modal" data-target="#myModal" class="btn btn-info btn-lg">Edit</a>
在创建元素之前从 div
function maketext(p) {
document.getElementById("mys").innerHTML = ''
// rest of the code
}