簡體   English   中英

如何使用動態生成的ID使用jQuery獲取標簽的值

[英]How to use dynamic generated id to get Value of label using jquery

我有一個GridView,其中有兩個控件,Label和Textbox。 我想在文本框的change事件上獲取Label的值。

我已經使用以下方法來做到這一點。 但我沒有得到價值

 $(document).ready(function () {
        $('[id$=txtInvoiceInDollar]').change(function (e) {
            var textboxid = this.id;

            var streetaddress = textboxid.substr(0, textboxid.lastIndexOf('_'));
            var exactId = streetaddress + '_lblSaleExt';
            var valuetes = $('"#"' + ctl00_ItemMain_grd_Invoice_ctl02_lblSaleExt + '"').val();
            alert(valuetes);

            return false;
        });
    });

HTML代碼是:

  <table cellspacing="0" cellpadding="2" border="0" style="border-collapse: collapse;" id="ctl00_ItemMain_grd_Invoice" class="result result-text">
        <tbody>
            <tr class="head">
                <th scope="col"><a href="javascript:__doPostBack('ctl00$ItemMain$grd_Invoice','Sort$PricingName')">Pricing Name</a></th>
                <th scope="col"><a href="javascript:__doPostBack('ctl00$ItemMain$grd_Invoice','Sort$Variation')">Variation</a></th>
                <th scope="col"><a href="javascript:__doPostBack('ctl00$ItemMain$grd_Invoice','Sort$StoreName')">StoreName</a></th>
                <th scope="col"><a href="javascript:__doPostBack('ctl00$ItemMain$grd_Invoice','Sort$Quantity')">Quantity</a></th>
                <th scope="col"><a href="javascript:__doPostBack('ctl00$ItemMain$grd_Invoice','Sort$ProductID')">Product</a></th>
                <th scope="col"><a href="javascript:__doPostBack('ctl00$ItemMain$grd_Invoice','Sort$Descr')">Description</a></th>
                <th scope="col"><a href="javascript:__doPostBack('ctl00$ItemMain$grd_Invoice','Sort$SalePrice')">Sale Price</a></th>
                <th scope="col">Sale Extended</th>
                <th scope="col"><a href="javascript:__doPostBack('ctl00$ItemMain$grd_Invoice','Sort$CostPrice')">Cost Price</a></th>
                <th scope="col">Invoice $</th>
                <th scope="col">Invoice %</th>
                <th scope="col"><a href="javascript:__doPostBack('ctl00$ItemMain$grd_Invoice','Sort$RemainingInDollar')">Remaining $</a></th>
                <th scope="col"><a href="javascript:__doPostBack('ctl00$ItemMain$grd_Invoice','Sort$RemainingInPercentage')">Remaining %</a></th>
                <th scope="col">Invoice $</th>
                <th scope="col">Invoice %</th>
            </tr>
            <tr class="odd">
                <td>Replacement of existing bla bla</td>
                <td>True</td>
                <td>KFC</td>
                <td>2</td>
                <td>Prod1</td>
                <td>Desc desc</td>
                <td>21.32</td>
                <td>
                    <span class="right" id="ctl00_ItemMain_grd_Invoice_ctl02_lblSaleExt">42.64</span>
                </td>
                <td>18</td>
                <td>
                    <span class="right" id="ctl00_ItemMain_grd_Invoice_ctl02_lblInvoiceInDollar">0</span>
                </td>
                <td>
                    <span class="right" id="ctl00_ItemMain_grd_Invoice_ctl02_lblInvoiceInPer">0</span>
                </td>
                <td>0</td>
                <td>0</td>
                <td>
                    <input type="text" class="right" id="ctl00_ItemMain_grd_Invoice_ctl02_txtInvoiceInDollar" value="0" name="ctl00$ItemMain$grd_Invoice$ctl02$txtInvoiceInDollar">
                </td>
                <td>
                    <input type="text" class="right" id="ctl00_ItemMain_grd_Invoice_ctl02_txtInvoiceInPer" value="0" name="ctl00$ItemMain$grd_Invoice$ctl02$txtInvoiceInPer">
                </td>
            </tr>

        </tbody>
    </table>

請幫助我,我如何獲得標簽的價值

謝謝你們,我已經自己修復了。

這是解決方案:

 $('[id$=txtInvoiceInDollar]').change(function (e) {
            var textboxid = this.id;

            var streetaddress = textboxid.substr(0, textboxid.lastIndexOf('_'));
            var exactId = streetaddress + '_lblSaleExt';
            var id2 = $("[id$=" + exactId + "]").text();
            alert(id2);

            return false;
        });

感謝您對此問題進行調查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM