将一个字符串传递给javascript函数

问题描述:

简单的问题。如何将一个字符串传递给一个javascript函数,并生成一个包含该字符串内容的弹出框警报?这里是我的:将一个字符串传递给javascript函数

test.html.erb文件。

<%= link_to 'Test','#', onclick: 'test(helloworld)', class: "btn btn-xs btn-primary" %> 

这里的test.js文件:

function test(name){ 
    alert(name); 
} 

然而,当我点击获取生成的 “测试” 环节, “HelloWorld” 的不弹出。什么都没发生。

helloworld在报价时,你通过它在:

onclick: 'test("helloworld")' 

我想象有在控制台某种错误也是如此。

+0

完美!谢谢。我知道这很简单。 – LewlSauce 2014-10-16 18:17:17

onclick: 'test(helloworld)' 

会将名为helloworld的变量传递给函数。相反,你想通过一个字符串文字,如:

onclick: 'test("helloworld")'