使用<select>的onchange()事件

解决方案 »

  1.   

    以下测试通过:<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>添加表格行</title>
    </head>
    <script language="javascript">
    function addRow()
    {//给表格加一行
    var tab = document.getElementById("tab");
    thisTr = tab.insertRow(-1);
    thisCell = thisTr.insertCell(-1);
    thisCell.innerHTML = "&nbsp;";
    }
    </script>
    <body>
    <select onchange="addRow()">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
    </select>
    <table width="200" border="1" cellspacing="0" cellpadding="0" id="tab">
      <tr>
        <td>&nbsp;</td>
      </tr>
    </table>
    </body> 
    </html> 
      

  2.   


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>添加表格行</title>
    </head>
    <script language="javascript">
    tempArray=new Object();
    function addRow(obj)
    {//给表格加一行
    if(!tempArray[obj.options[obj.selectedIndex].text]){
        var tab = document.getElementById("tab");
        thisTr = tab.insertRow(-1);
        thisCell = thisTr.insertCell(-1);
        thisCell.innerHTML = "&nbsp;";
        tempArray[obj.options[obj.selectedIndex].text]="1"
       }
    }
    </script>
    <body>
    <select onchange="addRow(this)">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
    </select>
    <table width="200" border="1" cellspacing="0" cellpadding="0" id="tab">
      <tr>
        <td>&nbsp;</td>
      </tr>
    </table>
    </body> 
    </html> 
      

  3.   

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>添加表格行</title>
    </head>
    <script language="javascript">
    var op=new Array();
    function addRow(obj)
    {//给表格加一行
    if(op[obj.options[obj.selectedIndex].index]!='added')
    {
        var tab = document.getElementById("tab");
        thisTr = tab.insertRow(-1);
        thisCell = thisTr.insertCell (-1);
        thisCell.innerHTML = "&nbsp;";
    op[obj.options[obj.selectedIndex].index] = 'added';
    }
    }
    </script>
    <body>
    <select onchange="addRow(this);">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
    </select>
    <table width="200" border="1" cellspacing="0" cellpadding="0" id="tab">
      <tr>
        <td>&nbsp;</td>
      </tr>
    </table>
    </body> 
    </html> -.-|\