Remove multiple children from parent?

假装没事ソ 提交于 2019-12-11 05:13:17

问题


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)


回答1:


<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();" /> 



回答2:


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]);
        }
    } 
}



回答3:


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

or if you want to change smthing before deleting then

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


来源:https://stackoverflow.com/questions/6795034/remove-multiple-children-from-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!