Javascript完全是业余爱好者?

Javascript完全是业余爱好者?,javascript,html,Javascript,Html,我编写了一个javascript函数,根据单击的单选按钮将选择框附加到单选按钮集。当一个单击一个或另一个时,它会执行此操作。问题是,我是一个如此业余的人,以至于我无法让它停止向我设置的跨度添加新的选择元素来放置它们。我一直在尝试不同的方法来检查元素或其内部HTML是否已设置,然后使用该检查来决定是否创建另一个select。所有这些对我都不起作用。谁能给我指点一下教程的方向吗?我一直在搜索,尝试了几乎所有的方法,没有fez,这让我觉得整个函数的结构是一个更简单的问题 这是代码,任何关于这方面的信息

我编写了一个javascript函数,根据单击的单选按钮将选择框附加到单选按钮集。当一个单击一个或另一个时,它会执行此操作。问题是,我是一个如此业余的人,以至于我无法让它停止向我设置的跨度添加新的选择元素来放置它们。我一直在尝试不同的方法来检查元素或其内部HTML是否已设置,然后使用该检查来决定是否创建另一个select。所有这些对我都不起作用。谁能给我指点一下教程的方向吗?我一直在搜索,尝试了几乎所有的方法,没有fez,这让我觉得整个函数的结构是一个更简单的问题

这是代码,任何关于这方面的信息都会非常有用。我只是自学了编码,所以我知道这很可怕。谢谢你的帮助

function moreInput(vInput)  {
var moreD = document.getElementById('moreD');
//document.write(moreD);
    if(vInput == \"approved\")  {
        if(moreD.length > 0)    {
            moreD.removeChild(approvedOpts);
        }
        var select = document.createElement(\"select\");
        select.options[0] = new Option('a');
        select.options[1] = new Option('b');
        select.options[2] = new Option('c');
        select.setAttribute(\"name\",\"approvedOptsA\");
    }
    if(vInput == \"denied\")    {
        //alert(moreD.innerHtml);
        //if(moreD.innerHtml === 'undefined')   {
            //moreD.removeChild(approvedOptsD);
            var select = document.createElement(\"select\");
            select.options[0] = new Option('x');
            select.options[1] = new Option('y');
            select.options[2] = new Option('z');
            select.setAttribute(\"id\",\"approvedOptsD\");
            select.
        //}
    }
moreD.appendChild(select);

}

如果只追加新选择,则需要在追加新选择之前删除旧选择。请尝试以下代码,我已删除您的注释行:

function moreInput(vInput)  {
    var moreD = document.getElementById('moreD');
    if(vInput == "approved")  {
        if(moreD.length > 0)    {
            moreD.removeChild(approvedOpts);
        }
        var select = document.createElement("select");
        select.options[0] = new Option('a');
        select.options[1] = new Option('b');
        select.options[2] = new Option('c');
        select.setAttribute("name","approvedOptsA");
    }
    if(vInput == "denied")    {
        var select = document.createElement("select");
        select.options[0] = new Option('x');
        select.options[1] = new Option('y');
        select.options[2] = new Option('z');
        select.setAttribute("id","approvedOptsD");

    }
moreD.removeChild(document.getElementById("approvedOptsD"));
moreD.removeChild(document.getElementById("approvedOptsA"));
moreD.appendChild(select);
它可能会抛出一些警告,我还没有测试过,但你应该知道你在寻找什么


是一篇关于使用javascript动态添加和删除dom节点的文章,虽然很老,但仍然准确且有用。

在添加任何新元素之前删除所有元素

此代码应适用于:

<script type=\"text/javascript\">

function moreInput(vInput)  {
    var moreD = document.getElementById('moreD');
        if(vInput == \"approved\")  {
            removeAllChildrens(moreD);
            var select = document.createElement(\"select\");
            select.options[0] = new Option('Comped Table');
            select.options[1] = new Option('Comped Drink');
            select.options[2] = new Option('Comped Cover');
            select.setAttribute(\"name\",\"approvedOptsA\");
        }
        if(vInput == \"denied\")    {
                removeAllChildrens(moreD);
                var select = document.createElement(\"select\");
                select.options[0] = new Option('Need to Reserve Table');
                select.options[1] = new Option('At Capacity');
                select.options[2] = new Option('Private Event');
                select.setAttribute(\"id\",\"approvedOptsD\");
        }
    moreD.appendChild(select);
}

function removeAllChildrens(cell) {
    if ( cell.hasChildNodes() ) {
        while ( cell.childNodes.length >= 1 ) {
            cell.removeChild( cell.firstChild );       
        } 
    }
}

 </script>

也许你可以去掉不必要的反斜杠,让它更容易阅读?即使是你自己,也不要回显长Javascript字符串,直接将其写入HTML或.js文件。不需要转义needed.my bad,所有html/前端内容都是由PHP输出的。只需将函数放在一个单独的js文件中进行简化,非常感谢。非常有用和完整!