如何使用动态生成的id来使用jquery获取标签的值

时间:2015-01-27 09:39:03

标签: javascript jquery asp.net textbox label

我有一个GridView,其中我有两个控件,Label和Textbox。我希望在文本框的更改事件中获得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>

请帮帮我,我怎样才能获得标签的价值

1 个答案:

答案 0 :(得分:0)

谢谢伙计们,我已经自己解决了这个问题。

这是解决方案:

 $('[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;
        });

感谢您对此问题的调查。