这是我第一次使用ASP.Net ...我不了解正在发生的某些事情,但是正在发生的事情是我有一个带有下拉菜单的asp.net gridview模板,一个复选框,文字,然后是一个值。有很多这样的行。选中特定复选框后,它应在其所在行的第4个“ td”中获取该值,并将该值乘以该行中的下拉列表的值。我在获取所选下拉选项的特定值时遇到麻烦。这是无效的代码行:
quantity = cell0.childnodes[1].value;
有人可以指出正确的方向来访问下拉菜单中的值吗?
<script>
function calculateTotal(chk) {
var grid = document.getElementById('<%= grvAddon.ClientID %>');
var inputs = grid.getElementsByTagName("input");
var totalcol1 = 0;
var quantity = 1;
//loop starts from 1. rows[0] points to the header.
for (i = 1; i < grid.rows.length; i++) {
//get the reference of first column
//cell is the checkboxes
cell = grid.rows[i].cells[1];
//cell4 is the values (text)
cell4 = grid.rows[i].cells[4];
//cell0 is the dropdowns
cell0 = grid.rows[i].cells[0];
//loop according to the number of childNodes in the cell
for (j = 0; j < cell.childNodes.length; j++) {
if (chk.type == "checkbox" && chk.checked) {
for (h = 0; h < cell4.childNodes.length; h++) {
if (cell4.childNodes[h].type == "text") {
quantity = cell0.childnodes[1].value;
totalcol1 += parseInt(cell4.childNodes[h].value * quantity)
}
}
}
}
}
var fee = $('[id$=txtHandlingFee]').val();
var policyclaims1 = document.getElementById('<%= lblPolicyPrice.ClientID %>').innerText;
totalcol1 = parseInt(totalcol1) + parseInt(policyclaims1) + parseInt(fee);
document.getElementById('<%= lblPricing.ClientID %>').innerHTML = totalcol1.toFixed(2).toString();
}
这里是我要访问的网格部分。
<asp:GridView ID="grvAddon" runat="server" AutoGenerateColumns="False" GridLines="None">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:DropDownList ID="cblAddon" runat="server" onchange ="calculateTotal(this);" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:GridView ID="GridView1" runat="server" CssClass="MyGV" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="MyDDL">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
您现在可以在具有类MyDLL
的GridView中获取具有类MyGV
的所有元素的值。
<script>
function calculateTotal() {
$('.MyGV .MyDDL').each(function (index, element) {
console.log('Row ' + index + ' value: ' + $(this).val());
});
}
</script>