使用jquery从asp.net控件中选择文本(innerhtml)

时间:2013-05-16 15:13:31

标签: jquery asp.net

我在每行上都有一个带有和图标的asp.net gridview,点击后我想创建一个带有可编辑文本框/输入字段的弹出框。这些文本框和输入字段将包含来自的现有文本我点击的行图标。

我以为我可以这样做,但它从类中获取文本,但不是从id看到底行代码:

$(".imgEditFields").click(function () {
        var row = $(this).closest("tr");
        var strServiceID = $(".clHiddenServiceID", row).text();
        var strTitleOfDoc = $(document).attr('title');
        var strSessionUser = strTitleOfDoc.replace("Order Tracking for ", "");
        var strPSTNNumber = $(".clHiddenPSTNNum", row).text();
        var strPSTNNum = strPSTNNumber.replace(/'/g, "");
        $('#popupTrackerEditFields h1:first').text("Edit Record " + strPSTNNum);
        var strSuppOrdNo = $("#lblSupplierOrderNum", row).text();

以下是我的评论,已经尝试了几个来自你们所有评论/回复的摘要

        $(".imgEditFields").click(function () {
        //I get the row with the icon that was clicked on
        var row = $(this).closest("tr");
        var strServiceID = $(".clHiddenServiceID", row).text();
        var strTitleOfDoc = $(document).attr('title');
        var strSessionUser = strTitleOfDoc.replace("Order Tracking for ", "");
        var strPSTNNumber = $(".clHiddenPSTNNum", row).text();
        var strPSTNNum = strPSTNNumber.replace(/'/g, "");
        $('#PopUpTrackerEditFields h1:first').text("Edit Record " + strPSTNNum);
        var strSuppOrdNo = $(".clSupplierOrderNumber", row).text();
        //All the above work great and i get what is expected, the problem begins blow when trying to select by //the id (in that row)

        //The below line achieves what i want visually but i cannot gaurantee its the correct row can i?
        $('#MainContent_tbPopUpEditSuppOrdNo').val(strSuppOrdNo);
        //The below line does not work as suggested by the replies and i have tried with HTML, val, Text etc
        $('#<%=tbPopUpEditSuppOrdNo.ClientID%>', row).html(strSuppOrdNo);
        //the blow works again not sure if it is selecting the correct row each time
        $('#MainContent_tbPopUpEditSuppOrdNo').attr('title', 'Edit your Supplier no here for ' + strPSTNNum);

2 个答案:

答案 0 :(得分:2)

这是由于.NET生成的元素ID。我会说要使用控件的ID,但是当你的控件在GridView中时这会很糟糕,并且是不可预测的。

最好的办法是在你的文本框上使用一个类(使用CssClass)并为行选择,就像你以前一样。

答案 1 :(得分:-1)

由于这些是ASP.NET控件,因此ID的呈现方式不同。您可以使用以下语法定位它们:

$("#<%= idOfControlHere.ClientID%>")

对于你的一个ID,这是一个例子:

$("#<%= lblSupplierOrderNu.ClientID %>", row).text();