当“更改”时,ajax 帖子不会发送jquery UI Sortable 中触发事件
为了便于阅读,我剪掉了部分代码。当通过拖动 col_1 的子元素触发更改事件时,将执行下面的 Positions 函数:
$('#col_1').Sortable(
{
change: positions,
}
);
问题是“positions”向服务器发送了一个 ajax post,但服务器没有通过:
function positions(){
**i've removed some javascript code leading to a javascript variable called "variable" ***
xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.open('POST', '/positions.php', true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("epositions="+variable);
xmlhttp.send(null);
return true;
}
因此 Post 不会发送到服务器。但我注意到,如果我改为
$('#col_1').Sortable(
{
change: function() { positions(); },
}
);
在单击 col_1 中子节点的句柄时发送 ajax post,但元素无法再排序。 having
change: function(data) { positions(); },
尽管元素已排序,但
也不会发送ajax 请求。我该如何克服这个问题?是我做错了什么还是 UI 可排序的问题?
I've snipped out parts of code for readability. The positions function below is executed when the change event is triggered by dragging on a child element of col_1:
$('#col_1').Sortable(
{
change: positions,
}
);
The problem is 'positions' sends an ajax post to the server which isn't going through:
function positions(){
**i've removed some javascript code leading to a javascript variable called "variable" ***
xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.open('POST', '/positions.php', true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("epositions="+variable);
xmlhttp.send(null);
return true;
}
So the Post isn't sent to the server. But I have noticed that if I instead put
$('#col_1').Sortable(
{
change: function() { positions(); },
}
);
The ajax post gets sent when the handle of a childnode in col_1 is clicked on, but the elements can no longer be sorted. Having
change: function(data) { positions(); },
does not send the ajax request either, although the elements are sorted.
How do I overcome this problem? Am I doing something wrong or is it a problem with UI sortable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我放置了一个可能对您有帮助的脚本。这是一个完整的例子,请点击这里:
XML POST 示例
(例如搜索 test.php< /强>)
I put a script that might help you. It is a complete example, click here:
XML POST EXAMPLE
(E.g. Search for test.php )
是的,我明白了,我还有其他怀疑(很抱歉,如果情况并非如此)。
这种怀疑是因为我没有看到 onreadystatechange 在你的代码中的位置。
正如您已经知道的那样, onreadystatechange 将调用您喜欢的某些函数,而不是相反。
我不明白为什么
必须在函数内部,这会让事情重新开始,并发现你自己在每次调用位置时都会遍历状态直到第 4 个状态。如果你看一下我的代码,它就超出了功能。我的代码中的事物如何分离是努力工作的结果,并且可以影响很多事情。
另请查看
onreadystatechange
的使用方式和位置。这就是我想提的所有内容:
处理 xml 请求(您希望它在最开始时只调用一次),因此位于函数内部看起来不是一个好主意(如果您考虑变量作用域,情况会更糟! !!!)
照顾好onreadystatechange 和其他 xml http 代码,您希望它在您的函数中,这样每次它都会带来新鲜的结果!!!
照顾好各州!
如果情况并非如此,我请求理解,并且答案没有帮助!
至于jquery我根本不使用它。
Yes I see, I have an other suspicion (sorry if this is not the case).
This suspission is because I don't see where onreadystatechange is in yor code.
As you already know it is the onreadystatechange that will call some function you like and not the other way around.
I don't see why the
must be inside a function, this will make thing start over and finding your self traveling through states until the 4 state every time you call positions. If you look at my code it is out of the function. How are things sepearetd in my code is a result of hard work and can affect things a lot.
Also check out how and where is used
onreadystatechange
.That's all what I wanted to mention:
Take care of the xml request (you want it called at the very beginnign only once) so being inside a function doesn't look a good idea (even worse if you consider variable scopes!!!!)
Take care of the onreadystatechange and other xml http code, you want it in your function so every time it brings fresh results!!!
Take care of the states!
I ask for understanding if all this is not the case and the answer is of no help!
As for jquery I don't use it at all.
change
回调是否需要return true;
才能工作?如果是这样,您可以更新代码以使用
change: function(data) { return Positions(); }
Does the
change
callback require areturn true;
to work?If so, you can update the code to use
change: function(data) { return positions(); }