简体   繁体   English

Javascript textarea字数不启用提交按钮

[英]Javascript textarea word count not enabling submit button

I am trying to make it so that when the user types more than 10 words into the textarea the submit button is enabled. 我试图使其成为当用户在textarea中键入超过10个单词时,启用提交按钮。 Below is my code exerpt. 下面是我的代码exerpt。 It's not letting me submit even when there are 10+ words in there. 即使有10个以上的单词,它也不会让我提交。 Any help would be appreciated. 任何帮助,将不胜感激。

            <?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>

You have used #content but you didn't define any id for your text area. 您已使用#content但未为文本区域定义任何id Define an ID and access it. 定义ID并访问它。 Add an id attribute like this, 添加这样的id属性,

echo "<textarea rows=35 cols=85 name=\"content\" id=\"content\">";

you had not defined the content as Id .. declare #content as id of text area.. 您没有将内容定义为Id ..声明#content为文本区域的id ..

"<textarea rows=35 cols=85 name=\"content\" id=\"content\">";

//finding the words count.. //找到单词计数..

String input = $('content').val();
String[] elements = input.split(" ");

if(elements.length>10){
//your code
}

Since this is a listener on the form, and the textarea has a name, you can read the value of the textarea as: 由于这是表单上的监听器,并且textarea具有名称,因此您可以将textarea的值读取为:

    var text = this.content.value;

.

    text = text.split(" ");

    // check for at least 10 words
    if(text.length < 10){

This will really only check if there are 10 spaces, if you want to check for something that looks more like a word, then look for strings of word characters: 这实际上只会检查是否有10个空格,如果你想检查看起来更像是单词的东西,那么查找单词字符串:

    var words = text.match(/\b\w+\b/g);

which will split on strings of word characters, then; 然后将分裂为单词字符串;

    if (words && words.length < 10) { 
        ...
var text = encodeURIComponent($("#content").val());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM