dsh77114 2016-05-20 11:49
浏览 142
已采纳

在javascript中使用onclick在使用onclick本身创建的div中创建一个新的div?

I created a div consisting of radio button inputs using onclick from a radio button input inside a form in php and then i again attempted to create another div using onclick from inside this created div but only the outer div is being created. Would like to know where i am wrong..

        <input id="sub"  type="radio" name="opt1" value="2" onclick="createDiv4()"> Academic User<br/>

           function createDiv4() {
            if(document.getElementById("acad_")==null)
            {
               var divi = document.createElement("div");
               divi.id="acad_";
                divi.style.height="100px";
                //divi.style.marginTop="200px";
               var cont = document.getElementById('contain');
                 cont.appendChild(divi);                   
                var p = document.createElement("label");
               var disp = document.createTextNode("Please select the type of User :");

               p.appendChild(disp);
               p.style.textSize="16px";
               p.style.textAlign="left";
               divi.appendChild(p);
               var b1 = document.createElement("BR");
               divi.appendChild(b1);

               var ip = document.createElement("input");
               ip.name="wo";
               ip.value="1";
               ip.type="radio";
               ip.onclick="createDiv4_1()";
               ip.style.marginRight="4px";

               divi.appendChild(ip);
               var labe = document.createElement("label");

               var labe_val = document.createTextNode("Technical Institute");
               labe.appendChild(labe_val);
               divi.appendChild(labe);

               var b = document.createElement("BR");
               divi.appendChild(b);

               var ip2 = document.createElement("input");
               ip2.name="wo";
               ip2.value="2";
               ip2.type="radio";
               ip2.style.marginRight="4px";
               ip2.onclick="createDiv4_1()";
               divi.appendChild(ip2);

                var labe2 = document.createElement("label");

               var labe_val2 = document.createTextNode("School");
               labe2.appendChild(labe_val2);
               divi.appendChild(labe2);


                if(document.getElementById("govt")!=null)
               {
                   var de = document.getElementById("govt");
                   de.parentNode.removeChild(de);
               }

            }   
            else
            {
                var divi1 = document.getElementById("acad_");
                divi1.parentNode.removeChild(divi1);
            }
        }

    </script>
    <script>    
        function createDiv4_1() {
            if(document.getElementById("school")==null)
            {

               var divi = document.createElement("div");
               divi.id="school";

                //divi.style.marginTop="200px";
               var cont = document.getElementById('contain');
                 cont.appendChild(divi);                   
                var p = document.createElement("label");
               var disp = document.createTextNode("Please select the type of User for  Workshop :");

               p.appendChild(disp);
               p.style.textSize="16px";
               p.style.textAlign="left";
               divi.appendChild(p);
               var b1 = document.createElement("BR");
               divi.appendChild(b1);

               var ip = document.createElement("input");
                ip.name="wo";
               ip.value="1";
               ip.type="radio";
               ip.style.marginRight="4px";

               divi.appendChild(ip);
               var labe = document.createElement("label");

               var labe_val = document.createTextNode("School Student");
               labe.appendChild(labe_val);
               divi.appendChild(labe);

               var b = document.createElement("BR");
               divi.appendChild(b);

               var ip2 = document.createElement("input");
               ip2.name="wo";
               ip2.value="2";
               ip2.type="radio";
               ip2.style.marginRight="4px";

               divi.appendChild(ip2);

                var labe2 = document.createElement("label");

               var labe_val2 = document.createTextNode("Teacher");
               labe2.appendChild(labe_val2);
               divi.appendChild(labe2);

               var b2 = document.createElement("BR");
                divi.appendChild(b2);

                var ip2 = document.createElement("input");
               ip2.name="wo";
               ip2.value="2";
               ip2.type="radio";
               ip2.style.marginRight="4px";

               divi.appendChild(ip2);

                var labe3 = document.createElement("label");

               var labe_val3 = document.createTextNode("Parent");
               labe3.appendChild(labe_val3);
               divi.appendChild(labe3);

                if(document.getElementById("govt")!=null)
               {
                   var de = document.getElementById("govt");
                   de.parentNode.removeChild(de);
               }

            }   
            else
            {
                var divi1 = document.getElementById("school");
                divi1.parentNode.removeChild(divi1);
            }
        }
        </script>
  • 写回答

2条回答 默认 最新

  • doulipi3742 2016-05-20 11:54
    关注

    You arent calling the functions properly. Instead of :

    ip.onclick= "createDiv4_1()";
    

    This is only a string, it won't call it. You put it in quotes in the HTML but in JavaScript you don't need to. Also get rid of the parenthesis in JS. So use this :

    ip.onclick= createDiv4_1;
    

    Working fiddle : https://jsfiddle.net/thatOneGuy/xgvqwcq4/2/

    I added a div with ID contain for this to work too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动