我嘗試建立一個註冊表單。該寄存器形式爲稱爲「地址1」的文本框,並在其附近有稱爲「添加地址」的按鈕。按鈕的功能 - 它爲更多地址添加文本框。先前地址框附近的按鈕變爲「刪除」,刪除地址框和按鈕。問題是:我必須按順序設置文本框 - 1,2,3,... - 我必須更改它們的名稱和ID。例如 - 如果我刪除地址欄編號4(名稱,編號=「地址4」),則所有下一個文本框的名稱和編號應該減少1.我嘗試在for循環中執行此操作。這很難解釋,所以我只是給你的代碼。嘗試在你的VS中編寫例如'document.getElementById(「address」+ n)。'你會發現你甚至在列表中看不到你可以在點號或名稱或值之後寫入的內容。我意識到這是因爲有一個變量,這是我想的問題。現在這裏的代碼:姓名和身份證號碼變化
<html>
<head>
<title>Untitled Page</title>
<script type="text/javascript">
var n1 = 1; //counting the buttons and divs. doesn't decrease.
//In another words - it counts the number of calls to Add()
var n3 = 1; //counting the address text fields.
//It being reduced and increased so that it will represent the exact number of text fields
function Add() {
n1++;
n3++;
s1 = "<div id='div" + n1 + "'>";
s1 += "Address <input type='text' name='address" + n3 + "' id='address" + n3 + "' />";
s1 += "<input type='button' name='add" + n1 + "' id='add" + n1 + "' value='Add Branch' onclick='Add();' /><br />";
s1 += "</div>";
var n2 = n1 - 1;
document.getElementById('div' + n2).insertAdjacentHTML('afterEnd', s1);
document.getElementById('add' + n2).onclick = function() { Remove(n2, (n3-1)); };
document.getElementById('add' + n2).value = 'Remove';
}
function Remove(nn1, nn2) { //nn1 - div number to remove. nn2 - the address field number in this div
var parent = document.getElementById('barcode');
var child = document.getElementById('div' + nn1);
parent.removeChild(child);
for (nn2 += 1; nn2 <= n3; nn2++) {
var n2 = nn2 - 1; //nn2 - current address text field. n2 - the new number for the address field
document.getElementById('address' + nn2).setAttribute('name', 'address' + n2);
document.getElementById('address' + nn2).setAttribute('id', 'address' + n2);
document.getElementById('address' + n2).setAttribute('value', 'address' + n2);
// try: document.getElementById('address' + nn2).name='address'+n2. doesn't work (for me)
}
n3--;
}
var check = false;
</script>
</head>
<body>
<form name='barcode' id='barcode' action="" >
<div id='div1'>
Address <input type='text' name='address1' id='address1' />
<input type='button' name='add1' id='add1' value='Add Branch' onclick='Add();' /><br />
</div></form>
</body>
</html>
對不起,這個代碼只有什麼鏈接到地址字段,我添加了評論。 非常感謝!
編輯: 我忘了你沒有看到這個代碼中的錯誤,因爲我已經試圖修復它。在我更改代碼之前,它先訂購了所有的div,文本字段和添加/刪除按鈕,以便所有現有的項目總是按照順序(1,2,3,...)編號。 如果您單擊2次添加按鈕,則可以看到以下代碼中的問題,然後刪除第一個地址字段,然後再次單擊添加按鈕。
<html>
<head>
<script type="text/javascript">
var n1 = 1;
function Add() {
n1++;
s1 = "<div id='div" + n1 + "'>";
s1 += "Address <input type='text' name='address" + n1 + "' id='address" + n1 + "' />";
s1 += "<input type='button' name='add" + n1 + "' id='add" + n1 + "' value='Add Branch' onclick='Add()' /><br />";
s1 += "</div>";
var n2 = n1 - 1;
document.getElementById('div' + (n2)).insertAdjacentHTML('afterEnd', s1);
document.getElementById('add' + (n2)).onclick = function() { Remove(n2); };
document.getElementById('add' + (n2)).value = 'Remove';
}
function Remove(n) {
var parent = document.getElementById('barcode');
var child = document.getElementById('div' + n);
parent.removeChild(child);
for (n += 1; n <= n1; n++) {
var n2 = n1 - 1;
document.getElementById('add' + n).onclick = function() { Remove(n2); };
document.getElementById('address' + n).setAttribute('name', 'address' + n2);
document.getElementById('add' + n).setAttribute('name', 'add' + n2);
document.getElementById('address' + n).setAttribute('id', 'address' + n2);
document.getElementById('add' + n).setAttribute('id', 'add' + n2);
document.getElementById('div' + n).setAttribute('id','div' + n2);
}
n1--;
document.getElementById('add' + n1).onclick = function() { Add() };
}
</script>
</head>
<body>
<form name='barcode' id='barcode' action="" >
<div id='div1'>
Address <input type='text' name='address1' id='address1' />
<input type='button' name='add1' id='add1' value='Add Branch' onclick='Add();' /><br />
</div>
</form>
</body>
</html>
爲什麼不只是總是刪除最後一個? –
你想用這個完成什麼?你只是想刪除文本框? –
這些都是動態的,你不必每次都改變所有的數字,只要確保新的數字與「最大」是唯一的。 – Hogan