主机百科
测评与优惠

weui textarea字数限制以及动态显示输入字数的方法(jquery版本)

1
  &lt;!DOCTYPE html&gt;  &lt;html&gt;  &lt;head&gt;  &lt;meta name=&quot;viewport&quot; content=&quot;width=device–width, initial–scale=1&quot;&gt;  &lt;link rel=&#39;stylesheet prefetch&#39; href=&#39;https://res.wx.qq.com/open/libs/weui/0.3.0/weui.css&#39;&gt;  &lt;/head&gt;  &lt;body&gt;  &lt;div class=&quot;weui_cells_title&quot;&gt;意见反馈&lt;/div&gt;  &lt;div class=&quot;weui_cells weui_cells_form&quot;&gt;  <span style=”font-family: Arial, Verdana, sans-serif;”>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;div class=&quot;weui_cell_bd weui_cell_primary&quot;&gt;  <span style=”font-family: Arial, Verdana, sans-serif;”>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;textarea class=&quot;weui_textarea&quot; id=&quot;textarea&quot; placeholder=&quot;请输入意见反馈&quot; rows=&quot;3&quot;&gt;&lt;/textarea&gt;  <span style=”font-family: Arial, Verdana, sans-serif;”>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;div class=&quot;weui_textarea_counter&quot;&gt;&lt;span id=&quot;count&quot;&gt;0&lt;/span&gt;/200&lt;/div&gt;  <span style=”font-family: Arial, Verdana, sans-serif;”>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/div&gt;  &lt;/div&gt;  &lt;script src=&quot;//cdn.bootcss.com/jquery/3.1.0/jquery.min.js&quot;&gt;&lt;/script&gt;  &lt;script&gt;  $(function(){  &nbsp; var max = 200;  &nbsp; $(&#39;#textarea&#39;).on(&#39;input&#39;, function(){  &nbsp;&nbsp;&nbsp;&nbsp; var text = $(this).val();  &nbsp;&nbsp;&nbsp;&nbsp; var len = text.length;  &nbsp;&nbsp;&nbsp;&nbsp; $(&#39;#count&#39;).text(len);  &nbsp;&nbsp;&nbsp;&nbsp; if(len &gt; max){  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $(this).closest(&#39;.weui_cell&#39;).addClass(&#39;weui_cell_warn&#39;);  &nbsp;&nbsp;&nbsp;&nbsp; }  &nbsp;&nbsp;&nbsp;&nbsp; else{  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $(this).closest(&#39;.weui_cell&#39;).removeClass(&#39;weui_cell_warn&#39;);  &nbsp;&nbsp;&nbsp;&nbsp; }  &nbsp; });  })  &lt;/script&gt;  &lt;/body&gt;  &lt;/html&gt;

完整方案,需要引用jquery。

未经允许不得转载:主机阁 » weui textarea字数限制以及动态显示输入字数的方法(jquery版本)

登录

找回密码

注册