gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:57:14 26 4
gpt4 key购买 nike

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

这是我用于添加和删除的 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);
}

有人可以抽出时间吗?我真的需要这个。谢谢你

最佳答案

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);
}

关于javascript - 如何使用 JavaScript 访问动态表单中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9825918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com