我正在尝试这样做,以便当用户在textarea中键入超过10个单词时,将启用提交按钮。下面是我的代码exerpt。即使有10个以上的单词,它也不会让我提交。任何帮助将不胜感激。
<?php
if($auth = 1)
{
echo "<center><h1>Write Article</h1><br /></center>";
echo "<form method=\"post\" action=\"processarticle.php\" id=\"myform\" >";
echo "<b>Keywords:</b> " . $array['keywords'];
echo "<br />";
echo "<b>Purpose:</b> " . $array['purpose'];
echo "<br />";
echo "<b>Style:</b> " . $array['style'];
echo "<br />";
echo "<b>Instructions:</b> " . $array['instructions'];
echo "<br />";
echo "<b>Length:</b> " . $array['length'];
echo "<br />";
echo "<hr>";
echo "<textarea rows=35 cols=85 name=\"content\">";
echo "</textarea>";
echo "<br />";
echo "<input type=\"hidden\" name=\"refferer\" value=\"1\" />";
echo "<input type=\"hidden\" name=\"articleid\" value=\"" . $arid . "\" />";
echo "<input type=\"hidden\" name=\"articletitle\" value=\"" . $articletitle . "\" />";
echo "<input type=\"submit\" value=\"Submit Article\" id=\"submit\" name=\"submit\" />";
echo "</form>";
}
?>
<br /><br /><br />
<!-- /main -->
</div>
<!-- content -->
</div>
<!-- /content-out -->
</div>
<!-- footer -->
<?php include($_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php'); ?>
</body>
<script type="text/javascript">
$('#myform').submit(function(event) {
var text = $("#content").val();
text = text.split(" ");
// check for at least 10 words
if(text.length < 10){
// prevent submit
event.preventDefault();
return false;
}
});
</script>
答案 0 :(得分:1)
您已使用#content
,但未为文字区域定义任何id
。定义ID并访问它。添加id
这样的属性,
echo "<textarea rows=35 cols=85 name=\"content\" id=\"content\">";
答案 1 :(得分:1)
您尚未将内容定义为ID ..
将#content
声明为文本区域的ID ..
"<textarea rows=35 cols=85 name=\"content\" id=\"content\">";
//找到单词count ..
String input = $('content').val();
String[] elements = input.split(" ");
if(elements.length>10){
//your code
}
答案 2 :(得分:0)
由于这是表单上的监听器,并且textarea具有名称,因此您可以将textarea的值读取为:
var text = this.content.value;
text = text.split(" ");
// check for at least 10 words
if(text.length < 10){
这实际上只会检查是否有10个空格,如果你想检查看起来更像是单词的东西,那么查找单词字符串:
var words = text.match(/\b\w+\b/g);
将分割为单词字符串,然后;
if (words && words.length < 10) {
...
答案 3 :(得分:0)
var text = encodeURIComponent($("#content").val());