gpt4 book ai didi

添加/删除字段的 Javascript 函数

转载 作者:行者123 更新时间:2023-12-01 14:15:25 26 4
gpt4 key购买 nike

我对 JavaScript 不是很有经验,但这段代码与我正在寻找的代码相似,尤其是它包含“删除字段”链接的地方。这是 Javscript 函数:

//Add more fields dynamically.
function addField(field,area,limit) {
if(!document.getElementById) return; //Prevent older browsers from getting any further.
var field_area = document.getElementById(area);
var all_inputs = field_area.getElementsByTagName("input"); //Get all the input fields in the given area.
//Find the count of the last element of the list. It will be in the format '<field><number>'. If the
// field given in the argument is 'friend_' the last id will be 'friend_4'.
var last_item = all_inputs.length - 1;
var last = all_inputs[last_item].id;
var count = Number(last.split("_")[1]) + 1;

//If the maximum number of elements have been reached, exit the function.
// If the given limit is lower than 0, infinite number of fields can be created.
if(count > limit && limit > 0) return;

if(document.createElement) { //W3C Dom method.
var li = document.createElement("li");
var input = document.createElement("input");
input.id = field+count;
input.name = field+count;
input.type = "text"; //Type of field - can be any valid input type like text,file,checkbox etc.
li.appendChild(input);
field_area.appendChild(li);
} else { //Older Method
field_area.innerHTML += "<li><input name='"+(field+count)+"' id='"+(field+count)+"' type='text' /></li>";
}
}

这是表单的 HTML:

<form name="frm" method="POST">
<strong>Neutral List</strong><br />
<ol id="neutrals_area">
<li><input type="text" name="neutral_1" id="neutral_1" /> <a style="cursor:pointer;color:blue;" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">Remove Field</a></li>
<li><input type="text" name="neutral_2" id="neutral_2" /> <a style="cursor:pointer;color:blue;" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">Remove Field</a></li>
<li><input type="text" name="neutral_3" id="neutral_3" /> <a style="cursor:pointer;color:blue;" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">Remove Field</a></li>
<li><input type="text" name="neutral_4" id="neutral_4" /> <a style="cursor:pointer;color:blue;" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">Remove Field</a></li>
<li><input type="text" name="neutral_5" id="neutral_5" /> <a style="cursor:pointer;color:blue;" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">Remove Field</a></li>
</ol>
<input type="button" value="Add Neutral Field" onclick="addField('neutrals_area','neutral_',0);" />
</form>

但是,我希望使用“删除链接”代码生成其他字段,而不仅仅是脚本中带有代码的字段。我知道必须调整自定义函数以包含该行为,但在尝试使该函数正常工作时遇到了各种麻烦。在 addField 函数底部的“} else {”后面的旧方法中,通过编辑代码来完成它似乎很简单:

} else { //Older Method
field_area.innerHTML += "<li><input name='"+(field+count)+"' id='"+(field+count)+"' type='text' /><a style="cursor:pointer;color:blue;" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">Remove Field</a></li>";
}

但是,我对如何将它包含在 W3C DOM 方法中感到困惑?

最佳答案

删除那些 if(!document.getElementById)if(document.createElement) 条件。每个浏览器都支持它们,而那些不支持的浏览器不值得支持(让它们抛出错误)。

要将删除方法添加到 DOM 创建的元素,只需使用 onclick 属性。您也可以使用 standard-compliant addEventListner() ,但这需要 IE 的一些解决方法。参见章节 Legacy Internet Explorer and attachEventOlder way to register event listeners .

所以代码是

...
li.appendChild(document.createTextNode(" ");
var a = document.createElement("a");
a.appendChild(document.createTextNode("Remove Field"));
a.style = "cursor:pointer;color:blue;";
a.onclick = function() {
// this.parentNode.parentNode.removeChild(this.parentNode);
// nay. we have better references to those objects:
field_area.removeChild(li);
};
li.appendChild(a);

http://jsfiddle.net/wtX7Y/2/

关于添加/删除字段的 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9823827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com