将 RoR 值传递到视图中的 javascript (JQuery)

发布于 2024-11-29 08:30:07 字数 912 浏览 1 评论 0原文

我对下面的代码感到困惑。有人可以告诉我我做错了什么,javascript 无法访问值 p 吗?谢谢 !

<% p=progress_for(contest_score) %> # p is non-zero

<script type="text/javascript">
  $(function(){
      $('#contest_progressbar').progressbar({value: <%=p%>}); 
      <!--value p becomes 0 when rendered by jquery-->

      $("#contest_progressbar").css({ 'background': 'LightYellow' });
      $("#contest_progressbar > div").css({ 'background': 'Orange' });
      $('#contest_progressbar span.text').text(<%=p%>+'%')
      <!--value p becomes 0 when rendered by jquery-->

   });
</script>
progress: <%=p%> progress bar should be below: # p is the correct value here

<div id='contest_progressbar' style="margin-left:20px; height: 20px;">
  <span class="text"></span>
</div>

编辑:使用直接脚本标签代替。但同样的问题 EDIT2:现在似乎是一个 JQuery 问题。请参阅 Bryan 答案评论中的 html

I've been puzzled by the following code. Could someone tell me what I'm doing wrong that javascript fails to access the value p ? Thanks !

<% p=progress_for(contest_score) %> # p is non-zero

<script type="text/javascript">
  $(function(){
      $('#contest_progressbar').progressbar({value: <%=p%>}); 
      <!--value p becomes 0 when rendered by jquery-->

      $("#contest_progressbar").css({ 'background': 'LightYellow' });
      $("#contest_progressbar > div").css({ 'background': 'Orange' });
      $('#contest_progressbar span.text').text(<%=p%>+'%')
      <!--value p becomes 0 when rendered by jquery-->

   });
</script>
progress: <%=p%> progress bar should be below: # p is the correct value here

<div id='contest_progressbar' style="margin-left:20px; height: 20px;">
  <span class="text"></span>
</div>

EDIT: Using straight script tag instead. Same problem though
EDIT2: Seems to be a JQuery problem now. See the html in the comment to Bryan's answer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林迷了鹿 2024-12-06 08:30:07

有可能 jQuery 没有加载吗?如果您检查调试器,它可能会给您一个提示,例如 $ is not Defined。如果存在 Prototype/jQuery 冲突(如果您在 v3.1 之前使用 Rails,则很有可能),可能会默默地失败。

也可能出现这样的情况:JS 代码块需要在加载 jQuery 之后出现。我认为这是问题的原因是,鉴于您提供的信息,类似于 $('#contest_progressbar span.text').text(5+'%');如果您已正确加载 jQuery 并且没有其他 ID 为 contest_progressbar 的元素,则应该始终可以工作。

示例: http://jsfiddle.net/6JQNe/1/

另外,还有一些小要点

  • :需要 <%= javascript_tag do %> (注意等号)以确保它呈现。
  • "#contest_progressbar > div" 不会匹配任何内容

Is it possible that jQuery just isn't loading? If you check in your debugger, it might give you a hint, something like $ is not defined. If there's a Prototype/jQuery conflict (highly possible if you're using Rails before v3.1), it may fail silently.

It may also be the case that JS code block needs to appear after you've loaded jQuery. The reason I'm thinking this is the issue is that, given the information you've provided, something like $('#contest_progressbar span.text').text(5+'%'); should always work if you have jQuery loaded correctly and no other elements with the ID contest_progressbar.

Example: http://jsfiddle.net/6JQNe/1/

Also, a few minor points:

  • You need <%= javascript_tag do %> (note the equals sign) to make sure it renders.
  • "#contest_progressbar > div" will not match anything
jJeQQOZ5 2024-12-06 08:30:07

问题已解决。它与 JQuery 没有太大关系。进度条的 div id 被破坏,因为代码位于部分代码中,由循环中的视图方法调用。每次调用都会覆盖同一个 div。下面的代码产生了我想要的结果。

<% pbar_id="pbar_#{contest_score.user_id}" %>

<script type="text/javascript">
  $(function(){
      $('#'+"<%=pbar_id%>").progressbar({'value':<%=p%>})
      $('#'+"<%=pbar_id%>").css({ 'background':'LightYellow' });
      $('#'+"<%=pbar_id%>"+' > div').css({ 'background':'Orange' });
   });
 </script>
progress: <%=p%>%
<div id=<%=pbar_id%> style="margin-left:20px; width:256px; height:20px;">
</div>

Problem fixed. It had nothing too do with JQuery. The div id for the progressbar was clobbered because the code is in a partial, which is called by a view method in a loop. Every calls keep overwriting the same div. The code below produces the result I want.

<% pbar_id="pbar_#{contest_score.user_id}" %>

<script type="text/javascript">
  $(function(){
      $('#'+"<%=pbar_id%>").progressbar({'value':<%=p%>})
      $('#'+"<%=pbar_id%>").css({ 'background':'LightYellow' });
      $('#'+"<%=pbar_id%>"+' > div').css({ 'background':'Orange' });
   });
 </script>
progress: <%=p%>%
<div id=<%=pbar_id%> style="margin-left:20px; width:256px; height:20px;">
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文