在 Rails 3 中从视图调用 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3342583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:53:21  来源:igfitidea点击:

Calling javascript functions from view in Rails 3

javascriptruby-on-rails

提问by kbjerring

I am new to Rails and I have a pretty simple problem with calling javascript functions from within a view. In Rails 2 I would do...

我是 Rails 的新手,我在从视图中调用 javascript 函数时遇到了一个非常简单的问题。在 Rails 2 中,我会做...

= javascript_tag "name(arguments)"

where the javascript function "name" was located in my application.js file. However, this does not appear to work in Rails 3? Or am I missing something? I have been searching Google for some time without finding an answer.

javascript 函数“名称”位于我的 application.js 文件中的位置。但是,这在 Rails 3 中似乎不起作用?或者我错过了什么?我一直在谷歌搜索一段时间没有找到答案。

UPDATE:

更新:

OK, so I looked at the source of the two different ways (using the javascript_tag and the haml javascript filter) as suggested. And this is very strange because the html source appears to be identical? Apart from a difference in double and single quotes in declaring the script type.

好的,所以我按照建议查看了两种不同方式(使用 javascript_tag 和 haml javascript 过滤器)的来源。这很奇怪,因为 html 源似乎是相同的?除了在声明脚本类型时双引号和单引号的区别。

FIRST:using the javascript_tag which does not work

第一:使用不起作用的 javascript_tag

= javascript_tag "number_interval(#{fact.current_value}, #{fact.growth_per_second}, #{fact.decimal_number}, '#{dom_id(fact, "number")}'"

Source...

来源...

<div id='number_number_interval_727'>loading</div>
<script type="text/javascript">
//<![CDATA[
number_interval(6952596670.36814, 2.33002440293917, 0, 'number_number_interval_727'
//]]>
</script>

SECOND:using the haml javascript filter and it works

第二:使用haml javascript过滤器,它可以工作

:javascript
  number_interval(#{fact.current_value}, #{fact.growth_per_second}, #{fact.decimal_number}, '#{dom_id(fact, "number")}')

Source...

来源...

<div id='number_number_interval_727'>loading</div>
<script type='text/javascript'>
//<![CDATA[
number_interval(6952596917.02179, 2.33002440293917, 0, 'number_number_interval_727')
//]]>
</script>

Well, I guess I'll just stick with the haml filter!

好吧,我想我会坚持使用haml过滤器!

采纳答案by D.Shawley

You have a syntax error:

你有一个语法错误:

= javascript_tag "number_interval(#{fact.current_value}, #{fact.growth_per_second}, #{fact.decimal_number}, '#{dom_id(fact, "number")}'"

is missing the closing parenthesis for the number_intervalfunction. I think that it should be:

缺少number_interval函数的右括号。我认为应该是:

= javascript_tag "number_interval(#{fact.current_value}, #{fact.growth_per_second}, #{fact.decimal_number}, '#{dom_id(fact, "number")}')"

回答by kbjerring

A friend of mine pointed me to the fact that there is a javascript helper in haml. Apparently, I can call javascript functions by using...

我的一个朋友向我指出,haml 中有一个 javascript 助手。显然,我可以通过使用调用 javascript 函数...

:javascript
  name_of_function(arguments)

This works, but of course only with haml.

这有效,但当然只适用于haml。

回答by Taryn East

Silly question but... is application.js included in your layout?

愚蠢的问题,但是... application.js 是否包含在您的布局中?

One option to try is to hand-code the js function-call into your view in "real" javascript - just to see if it works. eg

尝试的一种选择是在“真实”javascript 中将 js 函数调用手动编码到您的视图中 - 只是为了看看它是否有效。例如

<script type="text/javascript">
  name(arguments)
</script>

Then you can be sure it's not the js itself (or lack thereof) at fault.

然后你可以确定这不是 js 本身(或缺少它)的错误。

回答by Hitesh Manchanda

Check out the right syntax on link text

查看链接文本的正确语法

and Check if you have included the javascript defaults in your file.As this working for me in RAILS 3 also

并检查您是否在文件中包含了 javascript 默认值。因为这在 RAILS 3 中也适用于我