从父级中删除多个子级?

发布于 2024-11-25 17:18:12 字数 2437 浏览 1 评论 0原文

我有一堆同名的元素,我试图用 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 ( extraaccount)

I have a bunch on elements with the same name that i am trying to remove at the same time with an onchange function.

Here is the javascript:

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

(Not even sure if removeChildren is a real command) And my element that im giving the onchange action to:

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

And the elements im trying to have removed:

<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>

Sorry if the html is a bit sloppy but basically, a tbody with a bunch of tds that have the same name ( extraaccount)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

演出会有结束 2024-12-02 17:18:12
<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();" /> 
<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>

and...

<select id="piname" name="pi_name" onChange="removeoldAccounts();" /> 
可爱咩 2024-12-02 17:18:12

像这样的东西:

if ( e.hasChildNodes() )
{
    for(var i=0; i < e.childNodes.length; i++)
    {
        if(e.childNodes[i].nodeName == "extraaccounts") {
            e.removeChild(e.childNodes[i]);
        }
    } 
}

Something like this:

if ( e.hasChildNodes() )
{
    for(var i=0; i < e.childNodes.length; i++)
    {
        if(e.childNodes[i].nodeName == "extraaccounts") {
            e.removeChild(e.childNodes[i]);
        }
    } 
}
伤痕我心 2024-12-02 17:18:12

$('[name="extraaccounts"]').remove();

或者如果你想在删除之前更改一些内容

$('[name="extraaccounts"]').each(function(index,value){
//do smthing
$(value).remove
});

$('[name="extraaccounts"]').remove();

or if you want to change smthing before deleting then

$('[name="extraaccounts"]').each(function(index,value){
//do smthing
$(value).remove
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文