2

我有一堆同名的元素,我试图用 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 有点草率,但基本上,atbody有一堆td同名的 s (extraaccount)

4

3 回答 3

6
<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();" /> 
于 2011-07-22T19:31:48.313 回答
0

像这样的东西:

if ( e.hasChildNodes() )
{
    for(var i=0; i < e.childNodes.length; i++)
    {
        if(e.childNodes[i].nodeName == "extraaccounts") {
            e.removeChild(e.childNodes[i]);
        }
    } 
}
于 2011-07-22T19:31:53.087 回答
0

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

或者如果你想在删除之前改变一些东西

$('[name="extraaccounts"]').each(function(index,value){
//do smthing
$(value).remove
});
于 2011-07-22T19:35:27.363 回答