Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用表单提交按钮获得onclick?_Javascript_Ruby On Rails - Fatal编程技术网

Javascript 如何使用表单提交按钮获得onclick?

Javascript 如何使用表单提交按钮获得onclick?,javascript,ruby-on-rails,Javascript,Ruby On Rails,我有 @lesson.id%> “btn btn成功”,值=>“发送反馈”%> 特别是下面这行 <%= form_for(@lesson_feedback, remote: true) do |f| %> <%= f.select :feedback, options_for_select([["Great and helpful", "Great and helpful"], ["Clear and interesting but not what I'm lookin

我有


@lesson.id%>
“btn btn成功”,值=>“发送反馈”%>
特别是下面这行

<%= form_for(@lesson_feedback, remote: true) do |f| %>

<%= f.select :feedback, options_for_select([["Great and helpful", "Great and helpful"], ["Clear and interesting but not what I'm looking for", "Clear and interesting but not what I'm looking for"], ["Okay but not interesting","Okay but not interesting"], ["Unclear or confusing", "Unclear or confusing"] ]) %>

<!-- Lesson ID -->
<%= f.hidden_field :lesson_id, :value => @lesson.id %>

<%= f.submit :class => "btn btn-success" , :value => "Send feedback" %>

<!-- :confirm => 'Your confirm message' -->
<!-- :onclick => "clearContents();" -->

<% end %>
“btn btn成功”,值=>“发送反馈”%>
以上方法很好。不过,当我加上

<%= f.submit :class => "btn btn-success" , :value => "Send feedback" %>
“btn btn成功”,:onclick=>“clearContents();”,:value=>“发送反馈”%>
clearContents JS执行,但不再设置数据。JS是:

<%= f.submit :class => "btn btn-success" , :onclick => "clearContents();", :value => "Send feedback" %> 

函数clearContents(){
document.getElementById(“feedback_module”).innerHTML=“感谢您的反馈!感谢您抽出时间给我们留下反馈,我们正在尽最大努力改进我们的内容。”;
}

在没有答案的情况下,以下是我对它的看法:

--

Ajax

由于您使用的是,您需要捕获来自远程服务器的响应,从而允许您相应地在表单上填充反馈元素:

<script>
function clearContents() {
    document.getElementById("feedback_module").innerHTML = "Thanks for your feedback! We appreciate you taking the time to leave us feedback, and we're doing our best to improve our content.";
}

</script>
--

我想说的主要一点是,您希望确保您的javascript尽可能不引人注目。这是什么?它基本上是将javascript绑定和调用放在视图之外的地方

这是首选的,不,是推荐的,有几个原因——最显著的原因是,您最终会得到更多的应用程序。这使您能够灵活地在整个应用程序中创建和使用所需的所有功能,而无需将其限制为单个元素/功能块


所以底线是,您需要重新考虑您的代码,以便在视图范围之外实际工作。如果您这样做,那么您将能够像上面那样不引人注目地绑定调用,使其成为一个更高效的系统

哪些系统具有
id
反馈模块
?我们可以使用JQuery还是只使用JS?
#app/assets/javascripts/application.js
$(document).on("ajax:success", "#your_form", function() {
   $("#feedback_module").html("Thanks for your feedback");
});