使得从JavaScript Ajax请求的onclick [英] making an ajax request from javascript onclick
问题描述
我以前用的辅助方法link_to_remote和朋友创建链接或按钮,将发送Ajax请求的引擎盖下,更新网页的一些数据。
I am used to use the helper methods link_to_remote and friends to create links or buttons that will send Ajax request under the hood and update some data of the webpage.
我有一些它的背景图像按钮。我希望他们能够发送Ajax请求我滑轨服务器。 我想一个JavaScript的onClick +发送Ajax请求应该做的魔力,但我不知道的原型在Rails的助手方法是如何工作的。
I have some which background images are buttons. I want them to send Ajax request to my rails server. I guess a javascript onClick + sending Ajax request should do the magic but I have no idea of how Prototype works under the Rails helper method.
我希望这不是太蹩脚的问题
I hope this is not a too lame question
推荐答案
元素上的方法是观察
此外, $()
假设你正在使用的ID。如果你有一组按钮标记带班,而不是IDS,你可以使用 $$()
这需要一个CSS选择器和返回元素的数组。
The method on the element is observe
Also, $()
assumes you are using an id. If you have a set of buttons tagged with classes, rather than ids, you could use $$()
which takes a css selector and returns an array of elements.
您可以从拍摄效果,并迭代它,增加点击行为给每个按钮。
You could take the result from that and iterate over it, adding click behavior to each button.
$$(".bg_button").each(function(button){
button.observe('click', function(){
new Ajax.Request(url, {
method: 'get',
parameters: { key: 'value' },
onSuccess: function(res){
// handle the result
}
});
});
});
要详细了解工程样机,看看他们的API文档。我认为他们是pretty的好。
To learn more about how prototype works, look at their api docs. I think they are pretty good.
这篇关于使得从JavaScript Ajax请求的onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!