jQuery each - 将(this)与类规范组合 [英] jQuery each - combining (this) with class specification

查看:123
本文介绍了jQuery each - 将(this)与类规范组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历一些表格行。简化的行如下:

 < table> 
< tr id =ucf48>
< td class =ucf_text>
< input name =ucf_t48value =Ann变得非常参与她正在阅读的文本。 type =text>
< / td>
< / tr>
< tr id =ucf351>
< td class =ucf_text>
< input name =ucf_t351value =Ann是一个快速和自信的读者。 type =text>
< / td>
< / tr>
< / table>

我正在使用此代码循环:

  $('#ucf tr')。each(function(i,obj){
var cn = $(this).attr('id')。substr (3);
var t = $(this +'。ucf_text input')。val();

console.log(Row+ i);
.log(Cnum:+ cn);
console.log(t);
});

控制台输出为:

 行0 
Cnum:48
Ann变得非常参与她正在阅读的文本。
第1行
Cnum:351
Ann变得非常参与她正在阅读的文本。

现在在有人惹火我之前,我知道我可以通过参考我想要的数据使用'name'。但是,为什么我的cnum变量遵循'this',但t变量不?



谢谢。

解决方案

当你这样做:

  var t = $(this +'。ucf_text input ').val(); 

  var t = $(this) .find('。ucf_text input')。val(); 


I'm trying to cycle through some table rows. The simplified rows are as follows:

<table>
  <tr id="ucf48">
    <td class="ucf_text">
      <input name="ucf_t48" value="Ann becomes very involved in the text she is reading." type="text">
    </td>
  </tr>
  <tr id="ucf351">
    <td class="ucf_text">
      <input name="ucf_t351" value="Ann is a fast and confident reader." type="text">
    </td>
  </tr>
</table>

I'm using this code to cycle:

$('#ucf tr').each(function(i,obj){
    var cn=$(this).attr('id').substr(3);
    var t=$(this +'.ucf_text input').val();

    console.log("Row "+i);
    console.log("Cnum: "+cn);
    console.log(t);
});

The console output is:

Row 0
Cnum: 48
Ann becomes very involved in the text she is reading.
Row 1
Cnum: 351
Ann becomes very involved in the text she is reading.

Now before someone flames me, I know I can do this another way by referring to the data I want using 'name'. Why, however, does my cnum variable follow 'this' but the t variable does not?

Thanks.

解决方案

When you do:

var t=$(this +'.ucf_text input').val();

this isn't converting correctly to a string.

Try:

var t=$(this).find('.ucf_text input').val();

这篇关于jQuery each - 将(this)与类规范组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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