从rails视图文件中的脚本调用js文件中的函数

时间:2023-01-17 00:00:21

I am beginner to ruby on rails what I want to do in my application is call java script function from script define inside view file like in following manner

我初学者ruby on rails我想在我的应用程序中做什么就是调用java脚本函数从脚本定义里面的视图文件,如下面的方式

//code inside js file ...
$(document).ready(function() {
    alert("outside the function");
function abc()
{
    alert("inside function");
}
});

//code in side view file 
<div> creating view here </div>
<script type="text/javascript" language="javascript">
    abc();
</script>

But It not giving require output. It shows one alert i.e. outside the function. But not showing other one that mean not calling that function. I want to call that function ... How to do this? Any solution? Need help ... Thank you ....

但它没有给出要求输出。它显示一个警报,即在功能之外。但没有显示其他意味着不调用该功能的人。我想打电话给那个功能......怎么做?有解决方案吗需要帮助......谢谢....

1 个解决方案

#1


3  

Try this, you need to move your function abc() outside the jQuery ready wrapper function.

试试这个,你需要在jQuery ready包装函数之外移动你的函数abc()。

$(document).ready(function() {
    alert("outside the function");

});

function abc(){
    alert("inside function");
}

#1


3  

Try this, you need to move your function abc() outside the jQuery ready wrapper function.

试试这个,你需要在jQuery ready包装函数之外移动你的函数abc()。

$(document).ready(function() {
    alert("outside the function");

});

function abc(){
    alert("inside function");
}