【问题标题】:Access a hiddenfield inside gridview using jquery and update it?使用jquery访问gridview中的隐藏字段并更新它?
【发布时间】:2012-09-27 12:42:11
【问题描述】:

我在 asp.net 中有一个 gridview 其中一个字段是网格视图内的隐藏字段:

 <asp:TemplateField>
                                 <ItemTemplate>
                                      <input type="hidden" value="0" id="hdnIsChanged" runat="server" />
                                 </ItemTemplate>
                            </asp:TemplateField>

我在网格视图中也有一个单选按钮列表,带有一个正在工作的 jquery 点击事件...... 这是那个事件:

$("#MainContent_gvLineItems input[id*='rbAnswer']").click(function () {
                    var parentRow = $(this).parents('tr').eq(1) //used to get the row at index 1, parents('tr').length prints 3.  
                    //tr around the checkbox is index 2
                    //tr around row is index 1
                    //tr around header is index 0
                    //so we want to get a reference to index=1
                    var firstCell = parentRow.find('td:eq(0)'); //find the first cell
                    var p = $(this).parents("div[id='dMainAnswer']").find(".Answer:first"); //used to find the panel
                    var val = $(this).val();

                    switch (val) //check the value 
                    {
                        case 'No':
                            firstCell.css('background-color', 'red');
                            p.show();
                            break;
                        case 'Yes':
                            firstCell.css('background-color', 'green');
                            p.hide();
                            break;
                        case 'N/A':
                            firstCell.css('background-color', 'gray');
                            p.hide();
                            break;
                        default:
                            firstCell.css('background-color', 'transparent');
                            p.show();
                            break;
                    }
                });

这一切都很好,但是在这个点击事件中我想访问隐藏字段hdnIsChanged 我如何引用它?我试过了:

alert($('input[id$=hdnAnswered').val());

但它继续说未定义... 我希望能够在此单击事件中访问它并使用 jquery 为其设置一个值。 请记住它在 gridview 中,所以它出现在每一行...

感谢任何帮助。

【问题讨论】:

    标签: javascript jquery asp.net


    【解决方案1】:

    我个人会为您的隐藏字段分配一个类并尝试像这样访问它:

    $(this).closest(".myClass");
    

    请记住,由于这是一个服务器控件,因此该隐藏字段的 ID 很可能会在前面加上一堆 asp.net 垃圾。所以不要像这样渲染:

    <input type="hidden" value="0" id="hdnIsChanged" />
    

    它很可能会像这样呈现:

    <input type="hidden" value="0" id="clt100_clt100_290420349823049823423_hdnIsChanged" />
    

    【讨论】:

    • 我想到了这个让我试试,我会回复你的。
    • 嗯我试过这个alert($(this).closest(".hChanged").val()); 但它仍然说未定义...
    • 您单击的与隐藏输入相关的单选按钮在哪里?
    【解决方案2】:

    试试alert($("#hdnIsChanged").val())

    【讨论】:

    • 嗯,也许我误解了你要做什么。
    【解决方案3】:

    你试过了

    alert($('input[id$=hdnAnswered').val());
    

    但现在试试这个...你忘了关闭方括号

    alert($('input[id$=hdnAnswered]').val());
    

    这里是可以帮助您获取特定行的隐藏字段值的代码

      $("#MainContent_gvLineItems input[id*='rbAnswer']").click(function () {
                        var parentRow = $(this).parent('tr');
                        var hiddenField=parentRow.find('input[id$=hdnIsChanged]');
                        alert(hiddenField.val());
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 2011-09-17
      • 2014-01-13
      相关资源
      最近更新 更多