如何使用JQuery在JavaScript中计算单词 [英] How to count words in JavaScript using JQuery

查看:168
本文介绍了如何使用JQuery在JavaScript中计算单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的html文本框。当我提交文本框所在的表单时,我想使用Jquery获取一个带有单词数量的变量。我还想检查输入的文本是否只是字母,数字和连字符(也在jquery中)。在提交表单时,我不需要像用户输入那样计算单词。如果关闭jquery,表单将不会提交,所以我猜不使用php没有安全风险。这是真的吗?

I have a simple html text box. When I "submit" the form that the text box is in, I would like to get a variable with the number of words inside using Jquery. I would also like to check if the inputted text is only letters, numbers and hyphens (also in jquery). I do not need to count the words as the user types, just when the form is submitted. The form won't submit if jquery is turned off so I guess there are no security risks by not using php. Is this true?

HTML:

<input type='text' name='name' id='name' />
<input type='button' value='Sign Up' id='signUp'>

JQUERY(尝试):

JQUERY (attempt):

var wordcount = $('#name').val()  // i don't know how to count the words 


推荐答案

您可以拆分字符串,然后计算结果数组的长度。

You would split the string and then count the length of the resulting array.

$('input[type="submit"]').click( function() {
    var words = $('#name').val().split(' ');
    alert(words.length);
});

这篇关于如何使用JQuery在JavaScript中计算单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆