【问题标题】:jQuery selected grid cell not returning html() valuejQuery选择的网格单元不返回html()值
【发布时间】:2018-09-24 07:02:20
【问题描述】:

我有一个动态创建的 ASP.net 网格视图。我想返回用户单击的网格单元格的内容。在我看到的任何地方, html() 属性都应该这样做,但我的总是返回“未定义”。

我的设置是只有第一列会响应点击。我的代码:

$('#dgrMainGrid td:nth-child(1)').click(function() {
    var barCode = $('this').html();
    alert(barCode);
 })

根据来自不同网站的建议,我尝试了一些 hail-Marys,但一致认为 html() 应该可以工作。我想知道我选择单元格的方式是否错误 - 换句话说,我的“this”不是我想的那样。

感谢任何帮助。

编辑:

这是一个呈现的 HTML 行。

</tr><tr>
    <td>TD-00154</td>
    <td>IV PRobe</td>
    <td>Romex</td>
    <td>350 Under</td> 
    <td>217M01866</td>
</tr><tr>

【问题讨论】:

  • 查看 html 会有所帮助...您能向我们展示为 gridview 生成的 html 吗?
  • $(this) 不是$('this')
  • cris 说了什么。另外,尝试使用 $(document).on("click",''#dgrMainGrid td:nth-child(1)", function(){ var barCode = $(this).html(); });跨度>
  • 达格纳比特!我盯着这个愚蠢的错误看了 45 分钟。对不起,浪费大家的时间。感谢响应者。

标签: c# jquery asp.net gridview


【解决方案1】:

选择器我觉得不好,一定是$(this).html()

【讨论】:

    【解决方案2】:

    确保您在加载 DOM(文档对象模型)后调用您的 JS,否则在选择 dgrMainGrid 时大多数情况下您会得到 undefined

    $(document).ready(function(){
       // add your code hear
    });
    

    此外,当通过 ID 调用 ASP.net GridView 时,使用 &lt;%= dgrMainGrid.ClientID %&gt; 而不是 dgrMainGrid,因为 ID 是在 HTML 中自动生成的。

    【讨论】:

      猜你喜欢
      • 2016-06-18
      • 2021-03-11
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      • 2023-03-30
      相关资源
      最近更新 更多