简体   繁体   English

将新行添加到带有2个下拉列表的表中-如何更改选择的名称

[英]add new row to table with 2 drop down list- how do i change the name of the select

I would really appreciate if someone could help me. 如果有人可以帮助我,我将不胜感激。 for two days I've tried to figure this out alone but its not working. 有两天,我试图独自解决这个问题,但无法正常工作。

I'm writing a shopping list to my project at school. 我正在为学校的项目写一份购物清单。 I have a table (shopinglist) that contains the form. 我有一个包含表单的表格(购物清单)。

In the form I have 2 drop down lisst, one (product) is getting the values from my database and the second one (quantity) is constant numners. 在我有2个下拉列表的形式中,一个(乘积)从数据库中获取值,第二个(乘数)为常数。 Next to these two I have 2 buttons add and remove. 在这两个旁边,我有2个按钮添加和删除。

When pressing add I want to add a new row with the same stuff. 当按下添加时,我想添加具有相同内容的新行。

The problem is that I can't (or maybe don't know how to) change the select name according to the row number. 问题是我无法(或者可能不知道如何)根据行号更改选择名称。 I mean like 我的意思是

One last thing- the row will be added only if the user choose from both selection, not only from one. 最后一件事-仅当用户从两个选择中选择而不是从一个中选择时,才添加行。

here is my code: the javascript add and remove functions: 这是我的代码:javascript添加和删除功能:

 function addRow(tableID) {

if (document.getElementById("product").value!="dafult" && document.getElementById("quantity").value!="zero") 
{
            var table = document.getElementById(tableID);

        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        var colCount = table.rows[0].cells.length;

            var colCount = table.rows[0].cells.length;

            for(var i=0; i<=colCount; i++) {
             var newcell = row.insertCell(i);
                 newcell.innerHTML = table.rows[1].cells[i].innerHTML;
              switch(newcell.childNodes[1].type) {
                case "text":
                        newcell.childNodes[1].value = "";
                        break;
                case "checkbox":
                        newcell.childNodes[1].checked = false;
                        break;
                case "select-one":
                        newcell.childNodes[1].selectedIndex = 0;
                        break;

                }
            }
              newcell.childNodes[1].visible = true;
              table.rows[rowCount-1].cells[2].style.visibility="hidden" ;
            }
            else
          {
            if (document.getElementById("product").value!="dafult" && document.getElementById("quantity").value=="zero") 

                 alert("please choose quantity") ;
               if (document.getElementById("product").value=="dafult" && document.getElementById("quantity").value!="zero") 
                alert("please choose product") ; 
               if (document.getElementById("product").value=="dafult" && document.getElementById("quantity").value=="zero") 
                    alert("please choose product and qantity") ;
           }
            }

        function deleteRow(i){
        if (document.getElementById("product").value!="dafult" && document.getElementById("quantity").value!="zero") 
            document.getElementById('shopinglist').deleteRow(i)
        else
            alert("you can't delete row before adding it");
}

and the HTML and ASP 以及HTML和ASP

<table border="0" id="shopinglist">
<caption valign="top" style="width:90%;height:35px; color:green ;"/> your order 
<tr><td></td><td> product</td> <td> quantity</td></tr>
<tr>
<form name="juices">
    <%      
    set rs=Server.CreateObject("ADODB.recordset")
    rs.open "SELECT ID, [Product-Name] ,[size-liter],[Customer price] FROM Product GROUP BY  ID,[Product-Name] ,[Product].[size-liter],[Product].[Customer price] ", conn
    %>
<td>
<select name="product" id="product" width="10" >
<option value="dafult" selected>בחר מוצר מהרשימה</option>
<%
do while not rs.EOF %>
<option value=<%=rs("Product-Name")%> > juice<%=rs("Product-Name")%>   <%=rs("size-liter")%>  liter<%=rs("Customer price")%>  $</option>
<%
rs.MoveNext()
loop
rs.close %> 
</select>
</td>
<td>
<select name="quantity" id="quantity">
<option value="zero" selected="selected">0</option>
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
<option value="five">5</option>
<option value="six">6</option>
<option value="seven">7</option>
<option value="eight">8</option>
<option value="nine">9</option>
<option value="ten">10</option>
</select>
</td>
<td>
<input type="button" value="add" onclick="addRow('shopinglist')" id="addbutton"/>
</td>
<td>
    <input type="button" value="delete"onclick="deleteRow(this.parentNode.parentNode.rowIndex)" id="remove"/>
</td>
</tr>
         </form>

IE will not let you change the name of an element after you add it to the page. 将元素添加到页面后,IE将不允许您更改其名称。 The way this is written, you would have to actually alter the string containing the select before setting innerHTML 编写方式,您必须在设置innerHTML之前实际更改包含select的字符串

newcell.innerHTML = table.rows[1].cells[i].innerHTML.replace(/(\<select.+?name=".+?)"/, $1 + rowCount + i + "\"");

NOT TESTED... something like that... 未测试...类似的东西...

Here is an example https://gist.github.com/957827 that you could start with. 这是您可以开始使用的示例https://gist.github.com/957827 It is radically different but simpler than yours. 它与您完全不同,但更简单。

I don't have a IE6,7,8 to test it properly but it should be something like the example. 我没有IE6、7、8来正确测试它,但它应该类似于示例。

One more comment, id's must be unique. 还有一点评论,ID必须是唯一的。 And you shouldn't use them on a row of a table that you will copy. 而且,您不应该在要复制的表的一行上使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM