我必須通過匹配條件來制作元素。但是當我執(zhí)行另一個時,之前創(chuàng)建的元素會留在那里。如何使用java腳本避免這種重復? 我有這樣的 Javascript 函數(shù): 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>
提交后如何避免創(chuàng)建表單元素仍然存在?
猛跑小豬
2021-06-30 00:16:57