【问题标题】:symfony jQuery keyup strange situationsymfony jQuery keyup 奇怪的情况
【发布时间】:2012-06-06 12:19:20
【问题描述】:

我有简单的评论表格。我把 char-counter 。当我创建新评论时一切正常。当我尝试编辑评论时,字符计数器不起作用。我尝试了 Live(),但结果是一样的。编辑页面中的其他 Js 工作正常,只是这个 keyup 功能死了。我试图发出警报以查看 keyup 是否有效,但没有回应。 这是我的代码:

  • html

        <?php echo form_tag_for($form, '@comments',array('class' => 'nice'));?>
    
            <?php echo $form->renderHiddenFields() ?>
            <?php echo $form->renderGlobalErrors() ?>
            <?php echo $form['_csrf_token']; ?>
            <input type="hidden" name="comments[users_id]" id="comments_users_id" value="1" />
            <input type="hidden" name="comments[tests_id]" id="comments_users_id" value="<?php echo $testId?>" />
    
    
            <?php echo $form['comment']->renderError() ?>
    
              <div class="count">remaining symbols : 250</div>
              <div class="barbox"><div class="bar"></div></div>
    
            <?php echo $form['comment']->render(array('class' => 'comments_comment')) ?>
    
            <?php  echo $form['captcha']->renderLabel(null,array('class' => 'label-login-down ')) ?>
            <?php echo $form['captcha']->renderError() ?>
            <?php echo $form['captcha']->render(array('class' => 'normal  input-text ' , 'placeholder'=>"Въведете символите")) ?>
    
            <input type="submit" name="addComment" value="Изпрати" />          
        </form> 
    
  • jquery

    $(".comments_comment").keyup(function()
    {       
      var box=$(this).val();
      var main = box.length *100;
      var value= (main / 250);
      var count= 250 - box.length;
    
      if(box.length <= 250)
      { 
        if(box.length <=210)
        {
         $('.count').html('remaining symbols : '+count);            
        }
        else
        {          
       $('.count').html('<div class="commentAlertSymbols">remaining symbols :    '+count+'</div>'); 
        }
      $('.bar').animate(
      {
      "width": value+'%',
      }, 1);
    
    }
    else
    {
      $('.count').html('<div class="commentRedSymbols">remaining symbols : '+count+'</div>');
    }
    return false;
    });
    

【问题讨论】:

  • editnew 模板是否使用相同的表单渲染?当您编辑评论时,您的 comments textarea 是否真的有 .comments_comment 类?
  • 是的,bouth 表单与 .cmets_comment 完全相同,并且 bouth 中存在其他 css 类。我使用两种形式。我使用两个,因为新评论是在另一个模块中构建的。
  • 所以你对两个表单使用相同的表单渲染(一个用于创建,一个用于编辑)? JS 是否包含在两个渲染中?
  • 我在帖子中更新表格。我使用这个表格进行编辑和新建,并且路由做everiting。是的,我 tink js 包含在编辑表单中,因为表情符号和其他 js tink 工作正常。它们在同一个 js 文件中,带有 keyup 函数。我在 brouser 控制台日志中没有 js 错误
  • 你“认为” js 包括在内?仔细检查代码 :) 如果你的 keyup 也没有你的警报,那么你的 js 似乎没有加载

标签: javascript jquery symfony1 symfony-1.4


【解决方案1】:

如果能看到实际的表单 HTML 输出而不是所有那些 PHP 帮助函数,那就太好了,但我会试一试:

$('.comments_comment').keyup(function() {
    // Get the number of characters, percent used, and number left
    var $count = $('.count');
    var chars = $(this).val().length;
    var percent = (chars / 250) * 100;
    var left = 250 - chars;

    // Update the counter
    $count.html('Remaining Symbols: ' + left);

    // Add classes based on how many are left
    if (chars > 210 && chars <= 250) {
        $count.addClass('commentAlertSymbols');
    } else if (chars > 250) {
        $count.addClass('commentRedSymbols');
    }

    // Animate the bar
    $('.bar').css({
        width: chars <= 250 ? percent + '%' : '100%'
    });
});

我使代码更加干燥。另外,我将$('.bar').animate 行更改为$('.bar').css,因为我没有看到超过一秒的动画的意义。

您可以在 http://jsfiddle.net/KjTrj/ 看到一个有效的 JSFiddle。

【讨论】:

  • 嘿它有效! :)) Тhank you :)) 你为我节省了几个小时 :)
猜你喜欢
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-03
  • 2023-03-18
相关资源
最近更新 更多