jQuery each - combining (this) with class specification

☆樱花仙子☆ 提交于 2019-12-05 16:58:31

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();

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

You're trying to concatenate a string with a DOM node. I assume you want the children of each row? Which would be:
var t=$(this).find('.ucf_text input').val();

You cannot concatenate DOM objects and strings.

You can easily fix this by specifying this as the context of the selector:

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

By doing so the selector only matches elements inside the given context, i.e. the table row in your case.

You've already got 2 correct answers, but just for the sake of diversity, here's another way to do it:

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

Actually, this is because $(this) [tr] doesn't have the class .ucf_text

I think you meant the td beneath it

Add a space to signify you mean the child. :)

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

EDIT: Or not!

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

Find is way cooler, but I'm editing for the sake of not leaving a wrong answer up and wanted to be somewhat original, and besides... Maybe you need a distinct path to the input?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!