将 RoR 值传递到视图中的 javascript (JQuery)
我对下面的代码感到困惑。有人可以告诉我我做错了什么,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有可能 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 IDcontest_progressbar
.Example: http://jsfiddle.net/6JQNe/1/
Also, a few minor points:
<%= javascript_tag do %>
(note the equals sign) to make sure it renders."#contest_progressbar > div"
will not match anything问题已解决。它与 JQuery 没有太大关系。进度条的 div id 被破坏,因为代码位于部分代码中,由循环中的视图方法调用。每次调用都会覆盖同一个 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.