function addTest(){ 
  document.getElementById("add").innerHTML+="
商品品牌:<select id='s1' name='brand[]'>
            <option></option>
          </select> 
商品类别:<select id='s2' name='class[]'>
            <option></option>
          </select>商品货号:<input type=text name=shop_item[] style='width:100px; border-bottom:1p #000000 solid; border-right:none; border-left:none; border-top:none; width:80px;' />  数量:<input type=text name=shop_count[] style='width:40px; border-bottom:1px #000000 solid; border-top:none; border-left:none; border_right:none; width:80px;' /> 单价:<input type=text name=danjia[] style='width:40px; border-bottom:1px #000000 solid; border-top:none; border-left:none; border_right:none; width:80px;' /><br />"; 
  } 
  
  
html代码:<a  onClick="addTest()"><font style="cursor:hand;" color="#0033CC">添加商品</font></a> <br />商品品牌:<select id="s1" name="brand[]">
            <option></option>
          </select> 
商品类别:<select id="s2" name="class[]">
            <option></option>
          </select> 
商品货号:<input type="text" name="shop_item[]" style=" width:100px;border-bottom:1px #000000 solid; border-top:none; border-left:none; border-right:none; width:80px;" />
数量:<input type="text" name="shop_count[]" style=" border-bottom:1px #000000 solid; border-right:none; border-left:none; border-top:none; width:80px;"/> 
单价:<input type="text" name="danjia[]" style=" border-bottom:1px #000000 solid; border-right:none; border-left:none; border-top:none; width:80px;"/>商品品牌:<select id="s1" name="brand[]">
            <option></option>
          </select> 
商品类别:<select id="s2" name="class[]">
            <option></option>
          </select> 
商品货号:<input type="text" name="shop_item[]" style=" width:100px;border-bottom:1px #000000 solid; border-top:none; border-left:none; border-right:none; width:80px;" />
数量:<input type="text" name="shop_count[]" style=" border-bottom:1px #000000 solid; border-right:none; border-left:none; border-top:none; width:80px;"/> 
单价:<input type="text" name="danjia[]" style=" border-bottom:1px #000000 solid; border-right:none; border-left:none; border-top:none; width:80px;"/>请问,为什么我在javascript里面放上下拉列表就会报错?拿出来就没事呢?

解决方案 »

  1.   

    商品品牌: <select id="s1" name="brand[]"> 
                <option> </option> 
              </select> 
    商品类别: <select id="s2" name="class[]"> 
                <option> </option> 
              </select> 
    将这个放到
    function addTest(){ 
      document.getElementById("add").innerHTML+=" 
    商品品牌: <select id='s1' name='brand[]'> 
                <option> </option> 
              </select> 
    商品类别: <select id='s2' name='class[]'> 
                <option> </option> 
              </select>商品品牌: <select id="s1" name="brand[]"> 
                <option> </option> 
              </select> 
    商品类别: <select id="s2" name="class[]"> 
                <option> </option> 
              </select> 
    商品货号: <input type=text name=shop_item[] style='width:100px; border-bottom:1p #000000 solid; border-right:none; border-left:none; border-top:none; width:80px;' />  数量: <input type=text name=shop_count[] style='width:40px; border-bottom:1px #000000 solid; border-top:none; border-left:none; border_right:none; width:80px;' /> 单价: <input type=text name=danjia[] style='width:40px; border-bottom:1px #000000 solid; border-top:none; border-left:none; border_right:none; width:80px;' /> <br />"; 
      } 就会报错。这个怎么弄呀??
      

  2.   

    id 为 add 的控件在哪里?document.getElementById("add").
      

  3.   

    function addTest(){ 
      document.getElementById("add").innerHTML+=" 商品品牌: <select id='s1' name='brand[]'> 
                <option> </option> 
              </select> 
    商品类别: <select id='s2' name='class[]'> 
                <option> </option> 
              </select>商品品牌: <select id="s1" name="brand[]"> 
                <option> </option> 
              </select> 
    商品类别: <select id="s2" name="class[]"> 
                <option> </option> 
              </select> 商品货号: <input type=text name=shop_item[] style='width:100px; border-bottom:1p #000000 solid; border-right:none; border-left:none; border-top:none; width:80px;' />  数量: <input type=text name=shop_count[] style='width:40px; border-bottom:1px #000000 solid; border-top:none; border-left:none; border_right:none; width:80px;' /> 单价: <input type=text name=danjia[] style='width:40px; border-bottom:1px #000000 solid; border-top:none; border-left:none; border_right:none; width:80px;' /> <br />"; 
      } 这里有的呀、、、
      

  4.   

    字符串里怎么会有双引号呢,如:"商品类别: <select id="s2" name="class[]"> ",书写有点问题;
    报错信息是什么啊!!
      

  5.   

    商品品牌:<select id='s1' name='brand[]'>
                <option></option>
              </select> 
    商品类别:<select id='s2' name='class[]'>
                <option></option>
    我上面用的是单引号的。只不过复制错了。。不好意思、报错:一个是:缺少对象,一个是:未结束的字符串常量
      

  6.   

    把整个文件发给我看看:[email protected]
      

  7.   

    当然啦,你给的id都一样,这怎么可以呢,动态改变添加的id啊
      

  8.   

        这个问题, 你试试, 一行数据用一个变量 赋值,多用几个
      我刚才试试
         你那样是不行的。试试这样吧, 例如:
           var s = "商品品牌: <select id=\"s1\" name=\"brand[]\"> <option> </option> </select> ";
          var p = "商品类别: <select id=\"s2\" name=\"class[]\"><option> </option> </select> ";
         document.getElementById("add").innerHTML +=" <h1>fd5saf1sa1f561sdf1</h1>" + s + p;注意啊! 上面我的在编辑器里面能在一行放下,  你要在一行放下 可以多用几个变量  试试!
        我的可以
      你那样点击  网页有错误!
      

  9.   

    我回复你了,功能我不太了解,看了个大概!
    页面初始化是s1和s2两个,并且load了change方法,但是add新加的时候没有给他方法,且id要改变,如:添加一个就id变成了s3和s4;
    我稍微改了下,可能不太多,稍微改下应该就可以了;
      

  10.   

    订正一下:
    function addTest(){ 
      var n = s.length;
      var s1 = "s"+(n+1);
      var s2 = "s"+(n+2);
      s[n] = s1;
      s[n+1] = s2;
      document.getElementById("add").innerHTML+="内容省略了,是我改过的那个内容"; 
      for(var i=n;i<s.length-1;i++) {
        document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")"); 
      }
      change(0); //不知道是不是还是0
      

  11.   

    你把生成后的源码发来,php我运行不了!我调试看看
      

  12.   

    就是我上面贴的代码呀
    在点击<a  onClick="addTest()"> <font style="cursor:hand;" color="#0033CC">添加商品 </font> </a> <br /> 
    它的时候,在页面上就会增加这么几个:
     function addTest(){ 
      document.getElementById("add").innerHTML+="商品品牌:<select id='s1[i]' name='brand[]'><option></option></select>商品类别:<select id='s2[i]' name='class[]'><option></option></select>商品货号:<input type=text name=shop_item[] style='width:100px; border-bottom:1p #000000 solid; border-right:none; border-left:none; border-top:none; width:80px;' />  数量:<input type=text name=shop_count[] style='width:40px; border-bottom:1px #000000 solid; border-top:none; border-left:none; border_right:none; width:80px;' /> 单价:<input type=text name=danjia[] style='width:40px; border-bottom:1px #000000 solid; border-top:none; border-left:none; border_right:none; width:80px;' /><br />"; 
      } 
    其中包括,商品品牌、商品类别、数量、单价在页面上就会添加这个。。
      

  13.   

    楼主是这个意思吗
    我在EditPlus 下运行的<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
      <META NAME="Generator" CONTENT="EditPlus">
      <META NAME="Author" CONTENT="">
      <META NAME="Keywords" CONTENT="">
      <META NAME="Description" CONTENT="">
      <script> function addTest(){ 
    document.getElementById("info").innerHTML+="商品品牌: <select id='s1[i]' name='brand[]'> <option>123 </option> </select>商品类别: <select id='s2[i]' name='class[]'> <option> 3123</option> </select>商品货号: <input type=text name=shop_item[] style='width:100px; border-bottom:1p #000000 solid; border-right:none; border-left:none; border-top:none; width:80px;' />  数量: <input type=text name=shop_count[] style='width:40px; border-bottom:1px #000000 solid; border-top:none; border-left:none; border_right:none; width:80px;' /> 单价: <input type=text name=danjia[] style='width:40px; border-bottom:1px #000000 solid; border-top:none; border-left:none; border_right:none; width:80px;' /> <br />"; 
     } 
    </script>
     </HEAD>
    <body >
    <form id="form1"> 
    <table>
    <input type="checkbox"  name="myChkbox" />check
    <br>
    name:<input  name="input1" value="">
    <br>
    <input type="button" value="submit"  class="" onclick="mySubmit();" >
    <a  onclick="addTest();"> <font style="cursor:hand;" color="#0033CC">添加商品 </font> </a> <br > 
    <div id="info"> </div>
    </table>
    </form>
    </body>
    </HTML>
      

  14.   

    这个有点复杂 我简单的弄了下 基本上是可以取到值的,我现在取的是生成的文本框的值
    下拉框应该是一样的<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
      <META NAME="Generator" CONTENT="EditPlus">
      <META NAME="Author" CONTENT="">
      <META NAME="Keywords" CONTENT="">
      <META NAME="Description" CONTENT="">
      <script>
    function getValue(){
      alert(document.getElementsByName("danjia")[0].value)}
    function addTest(){ 
    document.getElementById("info").innerHTML+="商品品牌: <select id='s1[i]' name='brand'> <option>123 </option> </select>商品类别: <select id='s2[i]' name='class[]'> <option> 3123</option> </select>商品货号: <input type=text name=shop_item[] style='width:100px; border-bottom:1p #000000 solid; border-right:none; border-left:none; border-top:none; width:80px;' />  数量: <input type=text name=shop_count[] style='width:40px; border-bottom:1px #000000 solid; border-top:none; border-left:none; border_right:none; width:80px;' /> 单价: <input type=text id=test name=danjia style='width:40px; border-bottom:1px #000000 solid; border-top:none; border-left:none; border_right:none; width:80px;' /> <br />"; 
     } 
    </script>
     </HEAD>
    <body >
    <form id="form1"> 
    <table>
    <input type="checkbox"  name="myChkbox" />check
    <br>
    name:<input  name="input1" value="">
    <br>
    <input type="button" value="submit"  class="" onclick="getValue();" > <a  onclick="addTest();"> <font style="cursor:hand;" color="#0033CC">添加商品 </font> </a> <br > 
    <div id="info"> </div>
    </table>
    </form>
    </body>
    </HTML>
      

  15.   

    其实你动态生成的控件的名字不需要弄成数组的形式你把名字都设置成相同的就可以了
    然后  document.getElementsByName可以取到一个数组对象然后根据这个数组去取值就可以了
    document.getElementsByName("danjia")[0].value
    这个index 可以用一个循环得到
      for(i=0;i<document.getElementsByName("danjia").length;i++)   
      alert(document.getElementsByName("danjia")[i].value)应该没有问题了把