在页面加载时调用JavaScript中的method_helper

时间:2014-03-18 20:58:55

标签: javascript ruby-on-rails ruby

我得到了这个我在JavaScript中调用的method_helper。 Javascript可以通过按钮调用。

我的问题是Javascript只在我按下按钮时运行,但Ruby方法<%execute%>在它中只在页面加载时运行。

我真的不明白为什么它会在页面加载时运行,但是当我点击按钮时却没有。

Ruby方法在application_controller.rb

提前致谢!

我的按钮:

<button type="button" onclick="executeit()" class="btn btn-default">

我的Javascript在同一个.html:

function executeit() {
var selectingCommand = document.getElementById("CommandSelect");
 var selectedCommand = selectingCommand.options[selectingCommand.selectedIndex].text;
 var selectingServer = document.getElementById("serverlist");
 var selectedServer = selectingServer.options[selectingServer.selectedIndex].text;
 var username=document.getElementById("login").text;
  var password=document.getElementById("password").text;
  alert(selectedServer)
<%execute%>

}
</script>

1 个答案:

答案 0 :(得分:0)

这样的服务器端代码将在页面加载时运行,因为这是在服务器上生成事物的时候 - 它们在服务器上生成并发送到客户端。如果你在浏览器中查看源代码客户端,你会发现服务器端代码的输出会生成到javascript的那一部分。

执行javascript以响应按钮单击客户端,默认情况下不会使服务器端发生任何事情。

如果你想从这样的客户端javascript执行服务器端代码,你可能想看看某种AJAX解决方案。