带有Rails的入门级javascript:在表单提交时更新div(3.1/jquery) [英] Entry level javascript with rails: updating a div on form submit (3.1/jquery)
问题描述
我在Rails 3.1/jquery上有一个基本页面,其中包含一个包含文本的div,以及一个用于更新该文本的表单.我不太了解jquery或js,所以我只是在讨论控制器的coffeescript.
I have a basic page, with a div containing text, and a form to update that text, on rails 3.1/jquery. I do not know jquery or js well so I'm just poking about with the coffeescript for the controller.
我发现的所有教程似乎都不相关,而我发现的一些教程却很古老.我可以看到正在发生的事情,其中包括:我可以在ajax成功的情况下得到js警报,因为该表单作为远程表单(放在服务器上,并且控制器上的更新有效-基本文本已更改,但是我需要刷新页面才能看到更改.
None of the tutorials I've found seem be relevant, and the few I've found are ancient. I can see what is going on, sort of: I can get an js alert on ajax success, for the form as a remote form (that puts to the server and the update on the controller works -- the basic text gets changed, but I need to refresh the page to see the changes).
所以我要解决两个基本问题:
So I have 2 basic questions I'm working through:
-
我是否需要摆弄js局部文件,还是可以将其放入页面的js/coffeescript中?似乎不需要杂乱的分片就可以处理.
Do I need to fiddle with js partials or can I just put this into the js/coffeescript for the page? Seems like this can be handled without a messy bunch of partials.
您如何精确地控制使用js/jquery替换信息:.html,.load和其他几个功能似乎有牵连,但我没有找到合适的组合.
how exactly do you control replacing information with js/jquery: .html, .load, and several other functions seem to be implicated, but I haven't found the right combo.
我所发现的所有教程太旧或太冗长,以至于无法在Rails中完成一些基本的jquery东西.我正在尝试使用js/json缩减并加快表单提交的速度.对于初学者来说,新的,清晰的,简单的教程将是很棒的,但是任何指向我正确学习方向的东西都将是很棒的.
All the tutorials I've found are either too old or too verbose for a basic hit-the-ground- running understanding of doing some basic jquery stuff within rails. I'm trying to work toward paring down and speeding up form submissions with js/json. A newish, clear, simple tutorial for the uninitiated would be great, but anything pointing me in the right direction of learning would be great.
是的,我还没有发布任何代码,但是如果我能做一点工作,我会发布它.
Yeah, I've posted no code yet but if I can this little bit working, I'll post that.
推荐答案
在成功执行ajax的alert()
的地方,请执行以下操作:
Where you have the alert()
on ajax success, do something like this:
$("#myDivId").text(myText);
myText
是您提交给服务器的文本,还是来自服务器的响应-以最适合您的情况为准.
Where myText
is either the text you submitted to the server, or the response from the server - whichever is most appropriate for your situation.
更一般地说,要替换元素的文本,请使用 $("#myElementId").text("new text")
.要替换元素的HTML,请使用 $("#myElementId").html("<b>new HTML</b>")
More generally speaking, to replace the text of an element use $("#myElementId").text("new text")
. To replace the HTML of an element use $("#myElementId").html("<b>new HTML</b>")
这篇关于带有Rails的入门级javascript:在表单提交时更新div(3.1/jquery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!