【发布时间】:2012-06-12 10:33:56
【问题描述】:
我的页面中有多个名为“post”的 div 标签,我想做的是在提交点击时在服务器上发布数据。我无法在我的 jquery 方法 commentPost() 中检索 textarea "commentText" 的值。
<div id="post">
<br>topic containtment is here
<form name="postComment" id="commentForm" action="javascript:void(0);" method="post"
target="_top" onsubmit="return commentPost();">
<textarea name="comment" id="commentText" cols="10" rows="3" accesskey="1">
</textarea><br>
<input type="submit" name="submit" id="commentpost" value="Submit" accesskey="2">
</form>
</div>
jQuery 方法
function commentPost()
{
alert("Inside commentpost");
//how to get value of commentText
var comment=("form > commentText").val(); //<--not working
alert(comment);
//further code to be written
}
注意:页面中有多个div post标签。
如何获取textarea的值??
【问题讨论】:
-
“页面中有多个 div 帖子标签”... 那么您的标记无效。改为将其更改为类名。 ID 值必须是唯一的。
-
commentPost 方法有语法错误。应该是
var comment = $('form > #commentText').val()您缺少 $$sign 函数和指定类的主题标签。
标签: jquery function html jquery-mobile textarea