JQuery Rails 3:无法以简单形式执行 JS
javascript 和 Rails 的新功能。 当我的 Rails 应用程序不是表单时,我已经得到了一些简单的 jQuery 来与它一起使用。
现在,我尝试在用户点击表单中的“提交”时执行 jQuery 操作。 我遇到的问题是执行 HTML 代码而不是脚本。 顺便说一句:使用脚手架创建一个简单的表单。这是我想要获取的 CREATE 提交操作。
application.js:
jQuery.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript")
}
})
$(document).ready(function() {
$("#new_travel").submit(function() {
$.post(this.action, $(this).serialize(), null, "script");
return false;
});
create.js.erb:
alert('Yeah!'); // Have more, but just want to see the jQuery to be executed
new.html.erb:
<%= form_for(@travel, :remote => true) do |f| %>
..
..
<div class="actions">
<%= f.submit %>
travels 控制器在respond_to 块中保存format.js 有
任何关于问题所在的线索吗? 如果缺少任何信息,请告诉我。
谢谢!
New with javascript and Rails.
I've gotten some simple jQuery to work with my rails app when it's not a form.
Now I'm trying to do jQuery actions when the user hits 'Submit' in the form.
The issue I have is that HTML code is executed instead of the scripts.
BTW: Used scaffolding to create a simple form. It's the CREATE submit action I want to fetch.
application.js:
jQuery.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript")
}
})
$(document).ready(function() {
$("#new_travel").submit(function() {
$.post(this.action, $(this).serialize(), null, "script");
return false;
});
create.js.erb:
alert('Yeah!'); // Have more, but just want to see the jQuery to be executed
new.html.erb:
<%= form_for(@travel, :remote => true) do |f| %>
..
..
<div class="actions">
<%= f.submit %>
The travels controller holds the format.js in the respond_to block
Any clues as to what's wrong?
Let me know if any information is missing.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了!
我发现它确实适用于标准表单,但由于我操作的表单作为另一个 Jquery 调用的一部分显示在另一个页面内,因此 $("#new_travel") 没有检测到它。
我不得不将其更改为:
$("#new_travel").live('submit' function()...
然后它就工作了,因为页面上所有跟随 #new_travel 的内容都得到了 javascript 'hook'。
SOLVED!
I found out the it did actually work for standard forms, but as the form I operated on was shows inside another page as part of another Jquery call the $("#new_travel") did not detect it.
I had to change that to:
$("#new_travel").live('submit' function()...
And then it worked as all following #new_travel on the page got the javascript 'hook'.