jquery val() is not showing value of span

怎甘沉沦 提交于 2021-02-08 19:49:11

问题


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

回答1:


val() is not used for span or div it is used for input control like text, checkbox etc

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

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




回答2:


Your class name is commentpageno and you are listening on comment_page_no

Do

alert($('.commentpageno').text());

as .val() would not work for span.



来源:https://stackoverflow.com/questions/15953423/jquery-val-is-not-showing-value-of-span

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