jQuery .text()方法未从输入类型文本html元素获取任何文本 [英] jquery .text() method not getting any text from input type text html element

查看:104
本文介绍了jQuery .text()方法未从输入类型文本html元素获取任何文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DOM中找到这样的元素:

I find my element in the DOM like this :

tag_text_box = ($(this).parent().parent().find('.tags'))

我将其打印出来以检查它是否正确. 在这里:

I printed it out to check it is correct and indeed it is. here:

[
<input type=​"text" class=​"tags ui-autocomplete-input" style=​"width:​ 250px;​ height:​ 24px;​ background-color:​ red;​ " name=​"display_name" autocomplete=​"off" role=​"textbox" aria-autocomplete=​"list" aria-haspopup=​"true">​
]

但是tag_text_box.text()似乎根本不起作用.它不会打印我在输入中键入的任何文本.我应该使用其他方法吗?

However tag_text_box.text() seems not to be working at all. It doesn't print whatever text I have typed in my input. Should I use another method?

推荐答案

您需要使用 .val() 代替; .text()从元素内部获取文本,而.val()获取文本框的值,该文本框是输入的属性.

You need to use .val() instead; .text() gets the text from inside an element, whereas .val() gets the value of the text box, which is an attribute of the input.

您也可以在第一行中省略括号.请注意,您还应该使用var.当前,您正在全局命名空间中声明tag_text_box.它应该是这样的:

You can also leave the surrounding brackets out in your first line. Note that you should also be using var; currently, you're declaring tag_text_box in the global namespace. Here's what it should look like:

var tag_text_box = $(this).parent().parent().find('.tags');

var box_text = tag_text_box.val();


为简洁起见,您可以直接将输入值直接分配给变量:


To be more concise, you can simply assign the input's value straight to a variable:

var tags = $(this).parent().parent().find('.tags').val();

这篇关于jQuery .text()方法未从输入类型文本html元素获取任何文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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