我對JavaScript並不是很有經驗,但是這段代碼與我正在尋找的代碼類似,特別是它包含'刪除字段'鏈接的代碼。這裏是Javscript功能:Javascript函數添加/刪除字段
//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 { //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方法中?
謝謝。我會試試這個。這看起來很直接。一旦我理解了我正在做的事情的核心,我希望能讓它變得更加複雜。具體來說,一組三個字段可以根據需要添加或刪除(例如,汽車製造,引擎類型和製造年份) – 2012-03-22 14:41:16
您忘記了style屬性:-)已在http://jsfiddle.net中修復/ wtX7Y/ – Bergi 2012-03-22 14:41:35
爲了學習的目的,我同意使用純手工製作的js解決方案,但對於生產我看不出爲什麼不依賴乾淨,輕量且經過充分測試的庫。 – kappa 2012-03-22 14:53:48