只能添加 dom 元素但不能删除它们

发布于 2024-11-19 02:18:05 字数 1717 浏览 8 评论 0原文

我无法从 dom 中删除元素。我正在使用 firfox。我的代码看起来像这样

,我有一个表单,在提交之前应填写所有文本框。我的表单看起来像这样,

<form name="form" method="post" action="url" onsubmit="return checkEmpty(this)" >
<div class="field">
<label>field1</label><input type="text" name="f1" />
</div>
<div class="field">
<label>field2</label><input type="text" name="f2" />
</div>
..
....
......
input type="submit" name="Submit" value="submit" />
</form>

我在这里做的是,当提交表单时,我检查是否有任何文本框为空,如果有,我创建一个 dom 元素 h3 并将其附加到相应字段的 div 中,如果文本框不为空,我检查是否有任何文本框附加到相应的字段,如果找到,我将其删除。这是我的 javascript 代码

function checkEmpty(form) {
   var textboxes=form.elements;
   for(var i=0;i<textboxes.length;i++)
   {
      if((textboxes[i].type=="text" || textboxes[i].type=="password") && textboxes[i].textLength==0)                  //text box is empty,this code works in firefox but not in chrome                     
      {
         var msg=document.createElement('h3');
         msg.innerHTML="u cant leave this field blank";
         textboxes[i].parentNode.appendChild(msg);
         return false;
      }
      else                      //text box is not empty,this code neither works in firefox nor in chrome 
      {
         var rem_msg=textboxes[i].parentNode.getElementsByTagName('h3');
         for(var j=0;j<rem_msg.length;j++)
            textboxes[i].parentNode.removeChild(rem_msg[j]);
      }
   }
}

在我没有删除元素之前,代码工作正常,但删除元素是必要的,因为例如用户第一次尝试时没有在文本框 1 中输入任何值,因此将添加 h3 元素,但现在他输入了一些文本在第一个文本框中,但将第二个文本框留空,因此第一个文本框的 h3 元素应该消失。 代码看起来非常简单,我获取节点的引用,然后传递给 appendChildremoveChild。而且我什至无法在谷歌浏览器中向 dom 添加元素

i am not able to remove elements from the dom. i am using firfox. my code looks like this

i have a form for which all text boxes should be filled before submission. my form looks like this

<form name="form" method="post" action="url" onsubmit="return checkEmpty(this)" >
<div class="field">
<label>field1</label><input type="text" name="f1" />
</div>
<div class="field">
<label>field2</label><input type="text" name="f2" />
</div>
..
....
......
input type="submit" name="Submit" value="submit" />
</form>

what i do here is that when a form is submitted i check if any text box is empty and if there is any i create a dom element h3 and append it to the div of that corresponding field and if the text box is not empty i check if there is any text box attached to that corresponding field and if i find one i remove it. here is my javascript code

function checkEmpty(form) {
   var textboxes=form.elements;
   for(var i=0;i<textboxes.length;i++)
   {
      if((textboxes[i].type=="text" || textboxes[i].type=="password") && textboxes[i].textLength==0)                  //text box is empty,this code works in firefox but not in chrome                     
      {
         var msg=document.createElement('h3');
         msg.innerHTML="u cant leave this field blank";
         textboxes[i].parentNode.appendChild(msg);
         return false;
      }
      else                      //text box is not empty,this code neither works in firefox nor in chrome 
      {
         var rem_msg=textboxes[i].parentNode.getElementsByTagName('h3');
         for(var j=0;j<rem_msg.length;j++)
            textboxes[i].parentNode.removeChild(rem_msg[j]);
      }
   }
}

Until i was not removing elements the code was working fine but removing element is neccessary because for example a user doesnt enter any value in text box 1 on first attempt so an h3 element would be added but now he enters some text in first text box but leaves second text box empty so the h3 element of first text box should dissappear.
the code looks really simple, i am getting the reference of a node and then passing in to appendChild or removeChild. Also i am not even able to add elements to the dom in google chrome

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为守护你 2024-11-26 02:18:05

如果将产生错误的行更改为 if ((textboxes[i].type == "text" || textboxes[i].type == "password") && textboxes[i]. value == "") 可能会起作用

if you change the line that creates the error to if ((textboxes[i].type == "text" || textboxes[i].type == "password") && textboxes[i].value == "") possible will work

南渊 2024-11-26 02:18:05

试试这个:

 rem_msg[j].parentNode.removeChild(rem_msg[j]);

Try this one:

 rem_msg[j].parentNode.removeChild(rem_msg[j]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文