使用Java脚本删除动态元素 [英] Remove Dynamic Element With Javascript

查看:62
本文介绍了使用Java脚本删除动态元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Javascript制作了带有动态元素的表单。
我可以添加但无法删除!
我无法解决问题。我糊涂了! :(

I make a form with dynamic elements with Javascript. I can add BUT I can't remove! I can't Solve Problem. I'm confused! :(

我的代码:

My Code:

<script type="text/javascript">
//+Element
function addElement(div) {
  var ni = document.getElementById(div);
  var numi = document.getElementById('numVal');
  var num = (document.getElementById('numVal').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  newdiv.setAttribute('id',num);
  var unum="'"+div+"','"+num+"'";
  newdiv.innerHTML ='<input id="t" type="tel" placeholder="Tel.'+num+'" name="t'+num+1+'"><img id="del" onClick="removeElement('+unum+');" alt="del" src="images/del.gif" />';
  ni.appendChild(newdiv);
}
//-Element
function removeElement(divNum,div) {
  var d = document.getElementById(div);
  var olddiv = document.getElementById(divNum);
  d.removeChild(olddiv);
}
</script>

    <form>
    <input type="hidden" value="0" id="numVal" />
    <div id="m">
        <div id="h125">
            <div class="fb">
                <input id="t" type="tel" placeholder="Tel.">
                <img id="add" Alt="add" src="images/add.gif" onclick="addElement('h125');" />
            </div>
        </div>
    </div>
    </form>

谢谢

推荐答案

这是您要实现的目标吗?如果是这样,当您在 removeElement 函数中混合ID时。

Is this what you are trying to achieve? If so, when you mixed up ids in removeElement function.

//+Element
function addElement(div) {
  var ni = document.getElementById(div);
  var numi = document.getElementById('numVal');
  var num = (document.getElementById('numVal').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  newdiv.setAttribute('id',num);
  var unum="'"+div+"','"+num+"'";
  newdiv.innerHTML ='<input id="t" type="tel" placeholder="Tel.'+num+'" name="t'+num+1+'"><img id="del" onClick="removeElement('+unum+');" alt="del" src="images/del.gif" />';
  ni.appendChild(newdiv);
}
//-Element
function removeElement(divNum,div) {
  var d = document.getElementById(divNum);
  var olddiv = document.getElementById(div);
  d.removeChild(olddiv);
}

<form>
    <input type="hidden" value="0" id="numVal" />
    <div id="m">
        <div id="h125">
            <div class="fb">
                <input id="t" type="tel" placeholder="Tel.">
                <img id="add" Alt="add" src="images/add.gif" onclick="addElement('h125');" />
            </div>
        </div>
    </div>
    </form>

这篇关于使用Java脚本删除动态元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆