2013-03-06 203 views
0

我有一个textarea,我从JS使用Dojo library.The代码设置最大长度如下:textarea的最大长度值不工作

<textarea id="ct" rows="50" cols="50"></textarea> 

在JS的最大长度设置如下

var el = dojo.byId('ct'); 
if(el) { 
    dojo.attr(el,"maxLength",'2500'); 
    if (!('maxLength' in el)) { 
     var max = el.attributes.maxLength.value; 
     el.onkeypress = function() { 
      if (this.value.length >= max) 
       return false; 
     }; 
    } 
} 

我正在使用以下文本来填充textarea。

Twitter's character limit is 140. An SMS text message limit is 160. Google AdSense ads can have 25 characters for the title, 70 characters for the ad text, and 35 characters for the displayed URL. N.B. a space or punctuation are a "character". Exercice: try to copy and paste portions of this text to count the number of letters. * Even the cheapest online college demands a diploma. Studying an MBA program online or at online business schools requires a previous application where the number of letters is carefully controlled. Admissions to cheapest online college are regulated. Online business schools offer character counts in their advertising classes. An MBA program online offers a wide range of subjects, such as economics, organization, marketing, accounting, finance, strategic management, international business, management of information technology, human resources, and political strategies. Students work on a wide range of courses in the first year, then begin a specialization in the second. Specialization occurs through courses which are generally chosen by the student who wishes to explore a particular area (e.g. the market for finance options or pricing policies for marketing). * An MBA program online is one of the best in the world. It forms effective frameworks and established many links with the business world. American universities have considerable financial means. For example, Harvard's capital is 34.9 billion. In a federal country like the United States, the university system is decentralized and higher education institutions enjoy considerable autonomy that allows them flexibility. * Since the mid 1990s, the cheapest online colleges have undergone considerable consolidation. Many providers have gone out of business or were absorbed by larger groups. In 2005, there were 4,182 higher education institutions (colleges, universities, schools) in the United States, and approximately research 1,400 units. That represents the highest enrollment rate in higher education in the world. The two-year online colleges form the basis of tertiary education. * Online business schools enables Programme graduates to start their professional career abroad. In fact, multiple exchange agreements in many countries place online business schools among the World's most successful. The schools also develop and expand their networks constantly with MBA. The cultural mix is commonplace in the business school, with more than one in three students being from overseas. P 

当我粘贴以下网址它显示在text.Not肯定有什么原因,这是因为* 2501个characters.Is相同的文字textarea里获得最大的2500 characters.But的。

String Length

+0

在StackOverflow上有很多关于设置maxlength的例子[this](http://stackoverflow.com/questions/451491/what-is-the-best-way-to-emulate-an-html-input -max-length-attribute-on-html-t),[this](http:// stackoverflow。com/questions/451491/what-is-best-way-to-emulate-an-html-input-maxlength-attribute-on-html -t),[this](http://stackoverflow.com/questions/4459610/set-maxlength-in-html-textarea)等等。 – Naveen 2013-03-07 14:14:38

回答

2

如果您通过2499(dojo.attr(el,"maxLength",'2499');)更换您的最大长度它应该工作!

这是因为你必须从0开始计数!

希望我已经帮助你。

+0

我试了一下。但仍填充的文字仍然显示2501 characters.I复制它并粘贴在textarea填充,我猜这是因为UTF-8字符 – user521024 2013-03-07 06:26:33

0

而不是var max = el.attributes.maxLength.value;

您可以使用var max = el.attributes.maxLength; 现在完整的代码将是:

var el = dojo.byId('ct'); 
    if(el){ 
    dojo.attr(el,"maxLength",'2500'); 
    if (!('maxLength' in el)) { 
var max = el.attributes.maxLength; 
el.onkeypress = function() { 
    if (this.value.length >= max) return false; 
}; 
0

确实有包含多个字形UTF-16字符。例如,如果你复制下面的代码行,过去它在控制台,你会得到长度2:

"ñ".length; // Returns 2 

我建议你阅读this answer,它不是关于maxlength东西,但它解释了这些“星号“(包含多个字形的字符)。


现在,回到主题,您复制的字符串没有任何这些字符。当我复制并粘贴该虚拟文本时,它会给我2500个字符。你确定你没有意外地添加一个换行符到最后?当我第一次尝试时,我只是三次点击你的文字来选择它,然后粘贴到该网站上。

但是,它还在行的末尾添加了换行符号,因此也被计数。


而像Mithlesh Kumar在他回答说,retrievin的maxlength属性应该用做:

var max = el.attributes.maxLength; 

你不需要到.value添加到它,因为它返回的长度为普通数字。

如果您更改该部分代码的工作原理,至少在此JSFiddle