jquery val() 未显示跨度值 [英] jquery val() is not showing value of span
问题描述
在这里我试图获得 1 的跨度值 .text()
显示正确的值但 .val()
没有显示任何内容
Here i am trying to get value of span that is 1 here .text()
is showing proper value but .val()
is not showing anyting
<span class="commentpageno" id="commentpageno_1111" style="visibility: hidden">1</span>
$('.commentpageno').click(function (e) {
alert($('.comment_page_no').text());
alert($('.comment_page_no').val());
});
推荐答案
val() 不用于 span 或 div用于文本、复选框等输入控件
val() is not used for span or div it is used for input control like text, checkbox etc
.val() 方法主要用于获取表单元素的值例如输入、选择和文本区域.对于元素,.val() 方法返回一个数组包含每个选定的选项;如果未选择任何选项,则返回null, jQuery 文档
The .val() method is primarily used to get the values of form elements such as input, select and textarea. In the case of elements, the .val() method returns an array containing each selected option; if no option is selected, it returns null, jQuery docs
.text() 方法不能用于表单输入或脚本.设置或获取 input 或 textarea 元素的文本值,使用 .val()方法.要获取脚本元素的值,请使用 .html() 方法,jQuery 文档
The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method, jQuery docs
这篇关于jquery val() 未显示跨度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!