只能添加 dom 元素但不能删除它们
我无法从 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 元素应该消失。 代码看起来非常简单,我获取节点的引用,然后传递给 appendChild
或 removeChild
。而且我什至无法在谷歌浏览器中向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将产生错误的行更改为
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试试这个:
Try this one: