English 中文(简体)
j字字数确认
原标题:javascript word count form validations

我一直在尽力尝试并解决这一问题,但不能在奥古格尔找到任何解决办法,或者我的开端人(加拿大)的技巧不是任务。 因此,请有人向我表明,如果有人写了15个字或更多的话,我怎么能够把这份表格提交。 任何建议都会受到高度赞赏。 以下是 c、 j和html。

#container { 
    background: #ededed; 
    border: 1px solid #ddd; 
    margin: 0 auto; 
    padding: 10px;
    position: relative;
    width: 638px;
}

.error { color: #f00; }

ol.forms { list-style: none; overflow: hidden; }
ol.forms li { float: left; margin-bottom: 12px; width: 100%; }
ol.forms label { cursor: pointer; display: block; }
ol.forms textarea {
    border: 1px solid #ddd;
    float: left; 
    font: 14px/1.5em Georgia, Times, serif; 
    height: 120px; 
    margin-right: 5px;
    overflow-y: auto; 
    padding: 5px;
    width: 500px; 
}
ol.forms textarea:focus { border: 1px solid #000; }
ol.forms div.wordCount { float: left; font-size: 20px; line-height: 30px; }

#quick50 {
    background: #fff;
    border: 1px solid #ddd;
    font-size: 11px;
    left: 5px; 
    padding: 5px;
    position: fixed; 
    width: 188px; 
}

$(document).ready(function() {
    $("[class^= count[ ]").each(function() {
        var elClass = $(this).attr( class );
        var description = 0;
        var maxWords = 0;
        var countControl = elClass.substring((elClass.indexOf( [ ))+1, elClass.lastIndexOf( ] )).split( , );

        if(countControl.length > 1) {
            description = countControl[0];
            maxWords = countControl[1];
        } else {
            maxWords = countControl[0];
        }

        $(this).after( <div class="wordCount"><strong>0</strong> Words</div> );
        if(description > 0) {
            $(this).siblings( .wordCount ).addClass( error );
        }

        $(this).bind( keyup click blur focus change paste , function() {
            var text = jQuery.trim($(this).val()).replace(/s+/g," ");
            var numWords = text.split(   ).length;
            if($(this).val() ===   ) {
                numWords = 0;
            }   
            $(this).siblings( .wordCount ).children( strong ).text(numWords);

            if(numWords < description || (numWords > maxWords && maxWords != 0)) {
                $(this).siblings( .wordCount ).addClass( error );
            } else {
                $(this).siblings( .wordCount ).removeClass( error );    
            }

        });

    });
});

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jQuery Word Count</title>
<link href="wordcount.css" type="text/css" rel="stylesheet" media="screen,projection" />
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="wordcount.js"></script>
</head>

<body>
<div id="container">
    <h1>jQuery Word Count</h1>


    <div class="quickContain">
    <div id="quick50">
        <h2>50 words for quick copying</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam leo orci, porta eget, tincidunt sit amet, sagittis id, turpis. Aenean sed metus at leo ullamcorper dapibus. Duis vitae risus dignissim lectus dapibus faucibus! In porttitor, ante non adipiscing ultricies, magna velit blandit dui, a tempus ligula dolor eu orci? Sed.</p>
    </div>
    </div>

    <ol class="forms">
        <form action="submitcontent.php" method="post">

        <li><label for="minWord">Min Word Textarea <em class="help">(15 words or more)</em></label>
            <textarea onkeyup="limitWords(this,5);" name="minWord" class="count[15,0]" id="minWord"></textarea>
        </li>


        <input type="submit" value="Submit Content"  onclick="return doSubmit()" />
        </form>
    </ol>


</div>


</body>
</html>
最佳回答

我的头顶,像这样吗?

<textarea id="some-textarea" onkeypress="activateSubmit();">
</textarea>
<input type="submit" value="Submit" id="submit-button" disabled="disabled">

相应地:

<script>
  function activateSubmit(){
    var len = $( #some-textarea ).val().split(" ").length;
    if(len >= 15){ //15 words, hardcoded, probably change this to maxwords, or whatever.
      $( #submit-button ).removeAttr( disabled );
    }
    else{
      $( #submit-button ).attr( disabled ,  disabled );
    }
  }
</script>
问题回答

这是一个经常表达的例子:

$( form#count ).submit(function(){
    // if we accept that words are only made of letters
    if ($(this).find( textarea ).val().match(/[a-z]+/gi).length < 15) {
        // add some error message
        return false; // prevents submitting
    }
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签