我有一个大型 Ruby on Rails 表单,它具有以下部分 View 结构(文件 _form.html.erb)
<script language="javascript" type="text/javascript">
function myFunction1() {
... some js code here
}
function applyConfigs() {
... some js code here
}
</script>
<%= form_for(@test, :html => {:name => "Test", :id => "test_form"}) do |f| %>
... some Ruby code here
<div class="actions">
<input type=button value="Run Function1" onclick="myFunction1();">
</div>
... some Ruby code here
<div class="actions">
<input type=submit value="Apply" onclick="applyConfigs();">
</div>
<% end %>
在这种情况下,当我单击“Run Function1”或“Apply”按钮(按钮突出显示但不执行任何操作)时,不会调用任何函数。但是如果我将每个函数放在单独的
<script>
</script>
因此有 2 个脚本/脚本部分,每个部分包含一个函数,然后函数 myFunction1()
会正确执行,但 applyConfigs()
仍然不会(表单已提交,但applyConfigs()
函数未执行)。在任何情况下都没有错误消息,只是函数未执行。
知道这里发生了什么吗?
我的函数applyConfigs()
相当大,但这是我称为myFunction1()
的另一个函数
function showHideDiv(elementId,buttonId) {
if (document.getElementById) {
var element = document.getElementById(elementId);
var button = document.getElementById(buttonId);
if (element.style.display == 'none') {
element.style.display = 'block';
button.value = "Hide Options";
return false;
} else if (element.style.display == 'block') {
element.style.display = 'none';
button.value = "Show Options";
return false;
}
}
}
最佳答案
输入类型submit将提交表单。您需要更改它:
<div class="actions">
<input type="button" value="Apply" onclick="applyConfigs();">
</div>
关于javascript - 一个 JavaScript 函数运行,但两个函数不运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8500577/