如何使用javascript在动态表单中访问我的元素?

时间:2012-03-22 15:54:12

标签: javascript php html dynamic

我很抱歉我不知道如何访问这个: 我试图有一个按钮,当点击时,计算一个元素是一个数组的总量,因为它是动态的,但访问方法不起作用。

以下是我添加和删除的javascript:

function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;

    for(var i=0; i<colCount; i++) {
        var newcell = row.insertCell(i);
        newcell.innerHTML = table.rows[0].cells[i].innerHTML;

        switch(newcell.childNodes[0].type) {
            case "text":
                newcell.childNodes[0].value = "";
                break;
            case "checkbox":
                newcell.childNodes[0].checked = false;
                break;
            case "select-one":
                newcell.childNodes[0].selectedIndex = 0;
                break;
        }
    }
}

function deleteRow(tableID) {
    try {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;

        for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked) {
                if(rowCount <= 1) {
                    alert("Cannot delete all the rows.");
                    break;
                }
                table.deleteRow(i);
                rowCount--;
                i--;
            }
        }
    }catch(e) {
        alert(e);
    }
}

然后我有动态添加项目:

<tr>                            
    <td>Items</td>
    <td style="text-align:left;">
        <INPUT type="button" value="Add New Item" onclick="addRow('dataTable')" /> 
        <INPUT type="button" value="Delete Item" onclick="deleteRow('dataTable')" />
        <TABLE id="dataTable" width="350px" >
            <TR>
                <TD><INPUT type="checkbox" name="chk[]"/></TD>
                <TD>
                    <select name="ItemNo[]" id="select" value="ItemNo"  >
                        <?php         
                          $sql2="select * from jewelry_system.item where NumStored !='0' order by ItemName asc";
                          $result2 = mysql_query($sql2);
                                              while($row2=mysql_fetch_array($result2)){
                        ?>  
                        <option value="<?php echo $row2['ItemNo']?>:<?php echo $row2['SalePrice']?>"> <?php echo $row2['ItemName'];?>   [ Php <?php echo $row2['SalePrice'];?> Stocks Left:<?php echo $row2['NumStored'];?> ]</option>                                                          <?php } ?>
                        </select>
                </TD>
            </TR>
        </TABLE >           
    </td>                                           
</tr>

并使用此功能分离我的ItemNo值和项目的销售价格,然后我的功能不会加起来是checktotal():

function findexts(f){
    f=strtolower(f);
    exts=split('[/\\:]', f);
    n=count(exts)-1;
    exts=exts[n];
    return exts;
} 
function checkTotal(){
    var total = 0;
    var temp = 0;
    var itemArray = document.trans.ItemNo;

    for(var i=0;i<itemArray.length;i++){
        temp = findexts(itemArray[i].value);
        total += temp; 
    }   
    alert("Total Payment: "+total); 
}

有人可以随时待命。我真的需要这个。谢谢你

1 个答案:

答案 0 :(得分:0)

function checkTotal()
{
    var total = 0;
    var parts = null;

    // Your select input has an ID of "select", so this will iterate through each option
    $('#select option').each( function () {
        // The Item # and Sale Price are separated by a colon (:)
        parts = $(this).val().split(':');

        // Increment the total with the Sale Price of each item
        total += parseFloat(parts[1]);
    });

    alert("Total Payment: "+total);
}
//also The item $ will iterate so try this 
// The Item #,$ and Sale Price are separated by a colon (:)
        parts = $(this).val().split(':');

        // Increment the total with the Sale Price of each item
        total += parseFloat(parts[1]);
    });

    alert("Total Payment: "+total);
}