javascript 框架原型到 jquery
我发现以下脚本显然是使用 javascript 框架原型编写的。
Event.observe(window, 'load', function() {
Event.observe( 'btnSubmit', 'click', purchaseCD);
connectToServer();
});
function connectToServer()
{
new Ajax.Updater(
{ success: 'CD Count', failure: 'errors' },
'server_side.php',
{
method: 'get',
onSuccess: function(transport)
{
if (parseInt(transport.responseText)) connectToServer();
}
});
}
function purchaseCD()
{
new Ajax.Updater(
{ success: 'CD Count', failure: 'errors' },
'server_side.php',
{
method: 'get',
parameters: { num: $('txtQty').getValue() }
});
}
这里有人能够将此脚本转换为使用 jQuery 而不是原型吗?我根本不了解原型,所以我不明白它。
I have found the following script which is apparently written using the javascript framework prototype.
Event.observe(window, 'load', function() {
Event.observe( 'btnSubmit', 'click', purchaseCD);
connectToServer();
});
function connectToServer()
{
new Ajax.Updater(
{ success: 'CD Count', failure: 'errors' },
'server_side.php',
{
method: 'get',
onSuccess: function(transport)
{
if (parseInt(transport.responseText)) connectToServer();
}
});
}
function purchaseCD()
{
new Ajax.Updater(
{ success: 'CD Count', failure: 'errors' },
'server_side.php',
{
method: 'get',
parameters: { num: $('txtQty').getValue() }
});
}
Is anyone here able to convert this script to use jQuery instead of prototype? I don't know prorotype at all so I don't understand it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ajax.Updater
采用两个容器作为参数 1,它将更新对参数 2 中给出的 URL 的请求的成功或失败响应。该脚本的作用是在页面加载时(我下面将其翻译为 DOMReady(不完全相同,但 jQuery 约定),AJAX 请求发送到
server_side.php
。如果它收到它理解的响应,它会立即发送另一个请求,以保持会话处于活动状态。这看起来是一个糟糕的设计。如果您要做类似的事情,您肯定希望请求之间有一个超时。
该脚本的另一件事不太简洁,那就是每个 AJAX 请求都由同一页面 -
server_side.php
处理 - 依赖不同的参数来指示要执行的操作。简单地为不同的操作请求不同的页面会显得更干净。Ajax.Updater
takes, as parameter 1, two containers into which it will update the successful or failed response of a request to the URL given in parameter 2.What this script does is that upon page load (I translated it below to DOMReady which is not exactly the same, but jQuery convention) an AJAX request is sent to
server_side.php
. If it gets a response that it understands, it immediately sends off another request, in order to keep the session alive.This looks like a terrible design. If you're going to do something like that, you definitely want a timeout between the requests.
Another thing that's not very neat with this script is that every AJAX request is handled by the same page -
server_side.php
- relying on different parameters for instructions on what action to perform. It would appear cleaner to simply request different pages for different actions.