gpt4 book ai didi

javascript - 从父级中删除多个子级?

转载 作者:行者123 更新时间:2023-11-30 10:48:44 24 4
gpt4 key购买 nike

我有一堆同名的元素,我试图用 onchange 函数同时删除它们。

这是 JavaScript:

<script type="text/javascript">
function removeoldAccounts() {
var e = document.getElementById("account_table")
var accounts = document.getElementsByName("extraaccounts");
e.removeChildren(accounts);
}
</script>

(甚至不确定removeChildren是否是一个真正的命令)我的元素将onchange操作赋予:

<select id="piname" name="pi_name" onChange="removeoldAccounts" />

我试图删除的元素:

<tbody id="account_table">
<tr>
<td>Account Number<span>*</span>:</td>

<td id="accounts">
<select id="accountnum" name="account_number">
<option value="5636745946254">5636745946254</option>
<option value="23164847322">23164847322</option>
</select>
</td>

<td>
<input type="hidden" id="theValue3" value="81">
<input type="button" value="Add More" onclick="addaccount()">
</td>
</tr>

<tr id="80" name="extraaccount">
<td>
<select id="80" name="account_number">
<option value="5636745946254">5636745946254</option>
<option value="23164847322">23164847322</option>
</select>
</td>

<td>
<input type="text" size="20" name="account_comment80">
</td>

<td>
<input type="button" onclick="removeaccount(80)" value="remove">
</td>
</tr>

<tr id="81" name="extraaccount">
<td>
<select id="81" name="account_number">
<option value="5636745946254">5636745946254</option>
<option value="23164847322">23164847322</option>
</select>
</td>
<td>
<input type="text" size="20" name="account_comment81">
</td>

<td>
<input type="button" onclick="removeaccount(81)" value="remove">
</td>
</tr>
</tbody>

抱歉,如果 html 有点草率,但基本上,一个 tbody 和一堆具有相同名称的 td(额外帐户)

最佳答案

<script type="text/javascript">
function removeoldAccounts() {
var accounts = document.getElementsByName("extraaccounts");
var account;
var parent;
for (account in accounts) {
parent = account.parentNode;
parent.removeChild(account);
}
}
</script>

还有...

<select id="piname" name="pi_name" onChange="removeoldAccounts();" /> 

关于javascript - 从父级中删除多个子级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6795034/

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