在页面加载时调用JavaScript中的方法\u helper

在页面加载时调用JavaScript中的方法\u helper,javascript,ruby-on-rails,ruby,Javascript,Ruby On Rails,Ruby,我在JavaScript中调用了这个方法\u助手。Javascript支持通过按钮调用 我的问题是Javascript只在我按下按钮时运行,但其中的Ruby方法只在页面加载时运行 我真的不明白为什么它是在页面加载上运行的,但不是当我点击按钮时 Ruby方法在application_controller.rb中 提前谢谢 我的按钮: <button type="button" onclick="executeit()" class="btn btn-default"> 我的Jav

我在JavaScript中调用了这个方法\u助手。Javascript支持通过按钮调用

我的问题是Javascript只在我按下按钮时运行,但其中的Ruby方法只在页面加载时运行

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

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>
函数executeit(){
var selectingCommand=document.getElementById(“CommandSelect”);
var selectedCommand=selectingCommand.options[selectingCommand.selectedIndex].text;
var selectingServer=document.getElementById(“服务器列表”);
var selectedServer=selectingServer.options[selectingServer.selectedIndex].text;
var username=document.getElementById(“登录”).text;
var password=document.getElementById(“密码”).text;
警报(selectedServer)
}

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

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


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

谢谢,我会查一查的!