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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化