如何在jQuery中获取元素的文本值? [英] How to get an the text value of an element in jQuery?

查看:119
本文介绍了如何在jQuery中获取元素的文本值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态生成的HTML表,并且动态地为每一行生成了字段名.

I have an HTML table generated dynamically with field names generated for each row dynamically as well.

这是表格中的结构.我有一个<td>标记,在其中有一个<span>标记.我想获取每个标签的值.我已经尝试过这种方式:

Here is the structure I have within the table. I have a <td> tag and within it I have a <span> tag. I want to get the value for each tag. I have tried this this way:

$("#txtname").attr('value',$("#table1").find('td > #selname_'+i).text());

在上面的代码中,我使用<span>标记(selname)中的值设置$("#txtname")(文本框值). i是递增值.

In the above code I am setting the $("#txtname") (textbox value) with the value from the <span> tag (selname). Here i is an incrementing value.

我想获取文本,但不获取<span>值,因此我无法为其赋值.

I want to get the text, but do not get the <span> value, and I am not able to assign to it.

推荐答案

无需使选择器变得如此复杂:

No need to make your selectors so complicated:

$("#txtname").val($("#selname_"+i).text());

这篇关于如何在jQuery中获取元素的文本值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆