dpoppu4300 2013-03-11 21:39
浏览 52
已采纳

Codeigniter Textarea的jQuery最小字符计数器

I'm struggling to get a minimum character counter to work with CI / jQuery. I want to be able to prevent form submission and display an alert if the user has entered too few chars. The form just submits as normal using the code:

function checktextarea(minLength) {

  var textarea = jQuery('#field').value.length;
     if(textarea < minLength) {
        e.preventDefault();
        alert('You need to enter at least ' + minLength ' + characters');
        return false;
     }
};

And in CI I have:

<?php $attributes = array ('onsubmit' => 'checktextarea(15)'); ?>
<?php echo form_open('controller/function', $attributes); ?>

// I know this is standard HTML
  <textarea name="content" class="textarea_css" rows="10" cols="73" id="field"></textarea>

<?php echo form_submit('send', 'Submit Your Textarea'); ?>
<?php echo form_close(); ?>

Many thanks for any help!

  • 写回答

2条回答 默认 最新

  • dongqia3502 2013-03-11 22:25
    关注

    You have a reference to an e var, which isn't in the scope of your function. I'm assuming you're trying to cancel an event, but you should do that in your parent function.

    The line e.preventDefault(); causes compile errors.

    Next, you are accessing the value of the text area improperly, try jQuery('#field').val().length;.

    Finally, the line alert('You need to enter at least ' + minLength ' + characters'); is malformed, it should read alert('You need to enter at least ' + minLength + ' characters');

    Added a fiddle, just for fun and proof: http://jsfiddle.net/TSbK8/7/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)