dpoppu4300 2013-03-11 13: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 14: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部