我有我的文本框onchange事件的jquery函數。當觸發器,我想驗證由用戶輸入的值在文本框中的值由中繼器顯示的數據表。我能夠得到的文本框的價值,但我的問題是我不能得到中繼器的價值(數量列)。我爲該行的id添加了itemindex來標識正確的行並獲取列qty的值。在調試模式下,我使用的變量是未知的,所以我沒有得到任何價值。我需要幫助,現在我一直在爲此苦苦掙扎。如何使用jquery從Repeater獲取/訪問行值?
HTML
<asp:Repeater ID="rptInStock" runat="server">
<ItemTemplate>
<div id="ucprodres-in-stock<%# Container.ItemIndex%>>" class="ucprodres-in-stock">
<div class="ucprodres-stock manufacturer">
<%# CType(Container.DataItem, System.Data.DataRowView).Item("Mfg").ToString%>
</div>
<div id="instock-qtyavail" class="ucprodres-stock qty-avail">
<%# CType(Container.DataItem, System.Data.DataRowView).Item("Qty").ToString%>
</div>
<div class="ucprodres-stock availability">
<%# CType(Container.DataItem, System.Data.DataRowView).Item("Availability").ToString%>
</div>
<div class="ucprodres-stock unit-price">
<%# CType(Container.DataItem, System.Data.DataRowView).Item("QtyRange").ToString%>
</div>
<div class="ucprodres-stock dollar">
<%# CType(Container.DataItem, System.Data.DataRowView).Item("QtyRangePrice").ToString%>
</div>
<div class="ucprodres-stock qty-required instock-qtyreqd">
<asp:TextBox ID="tbxInStock" runat="server"></asp:TextBox>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
jQuery的:(?+的ItemIndex是不是也工作了,我怎麼添加索引到ID,因爲我用container.itemIndex讓我的ID是唯一的)
function ValidateQty1(txtBox) {
var qtyreqd = txtBox.value;
var instockrow = document.getElementById("ucprodres-in-stock" + itemIndex);
var qtyavail = instockrow.childNodes[1].nodeValue;
if (qtyreqd > qtyavail) {
alert("The required quantity should not be greater than the available quantity");
}
樣本生成html
<div id="ucprodres-in-stock">
<div id="ucprodres-in-stock0>" class="ucprodres-in-stock">
<div class="ucprodres-stock manufacturer">
FAIRCHILD SEMICONDUCTOR C
</div>
<div id="instock-qtyavail" class="ucprodres-stock qty-avail">
2
</div>
<div class="ucprodres-stock availability">
In Stock
</div>
<div class="ucprodres-stock unit-price">
</div>
<div class="ucprodres-stock dollar">
</div>
<div class="ucprodres-stock qty-required instock-qtyreqd">
<input name="MasterBasic$MasterPageContent$ucProductHeader$ucProductResults$rptInStock$ctl00$tbxInStock" type="text" id="MasterPageContent_ucProductHeader_ucProductResults_rptInStock_tbxInStock_0" onchange="ValidateQty1(this);" onkeypress="return validate(event);" />
</div>
</div>
<div id="ucprodres-in-stock1>" class="ucprodres-in-stock">
<div class="ucprodres-stock manufacturer">
Fairchild Semiconductor Corp
</div>
<div id="instock-qtyavail" class="ucprodres-stock qty-avail">
6
</div>
<div class="ucprodres-stock availability">
In Stock
</div>
<div class="ucprodres-stock unit-price">
</div>
<div class="ucprodres-stock dollar">
</div>
<div class="ucprodres-stock qty-required instock-qtyreqd">
<input name="MasterBasic$MasterPageContent$ucProductHeader$ucProductResults$rptInStock$ctl01$tbxInStock" type="text" id="MasterPageContent_ucProductHeader_ucProductResults_rptInStock_tbxInStock_1" onchange="ValidateQty1(this);" onkeypress="return validate(event);" />
</div>
</div>
下面是一個輸出示例,以更好地理解我的情景:當onchange觸發時,我想在txtbox改變的地方獲得價值。
更新: jQuery函數:
function ValidateQty1(txtBox) {
var qtyreqd = txtBox.value;
var qtyavail = $(this).parent().siblings(".qty-avail").text().trim();
alert(qtyavail);
}
HTML:
<asp:Repeater ID="rptInStock" runat="server">
<ItemTemplate>
<div id="ucprodres-in-stock<%# Container.ItemIndex%>" class="ucprodres-in-stock">
<div class="ucprodres-stock manufacturer">
<%# CType(Container.DataItem, System.Data.DataRowView).Item("Mfg").ToString%>
</div>
<div id="instock-qtyavail" class="ucprodres-stock qty-avail">
<%# CType(Container.DataItem, System.Data.DataRowView).Item("Qty").ToString%>
</div>
<div class="ucprodres-stock availability">
<%# CType(Container.DataItem, System.Data.DataRowView).Item("Availability").ToString%>
</div>
<div class="ucprodres-stock unit-price">
<%# CType(Container.DataItem, System.Data.DataRowView).Item("QtyRange").ToString%>
</div>
<div class="ucprodres-stock dollar">
<%# CType(Container.DataItem, System.Data.DataRowView).Item("QtyRangePrice").ToString%>
</div>
<div class="ucprodres-stock qty-required instock-qtyreqd">
<input type="text" onkeypress="return validate(event);" onchange="ValidateQty1(this);" />
<%--<asp:TextBox ID="tbxInStock" runat="server"></asp:TextBox>--%>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
檢查更新的小提琴http://jsfiddle.net/X5Q4H/2/ – Dalorzo