将数组值分配给页面上的元素
我对 jQuery 还是有点陌生,需要一些帮助。
我正在使用如下拖放脚本。
$(document).ready(function() {
$(function() {
$("#contentLeft ul").sortable({
opacity: 0.6,
cursor: 'move',
update: function() {
var order = $(this).sortable("serialize") + '&action=updateRecordsListings';
$.post("updateDB.php", order, function(theResponse) {
$("#contentRight").html(theResponse);
});
}
});
});
});
一旦拖放完成并且 php 服务器端脚本已解析,theResponse 就会包含一个如下所示的动态数组。
Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 2
)
我需要做的是一旦我得到响应 - 重命名相应 div 的 div id,因此顶部的将得到 QuestionOrder_0,下一个 QuestionOrder1 下一个 QuestionOrder2 等等。
页面加载时的 div 是动态创建的,如下所示。
<div id="questionOrder_0" class="questionHolder"> ...some stuff here ... </div>
<div id="questionOrder_1" class="questionHolder"> ...some stuff here ... </div>
<div id="questionOrder_2" class="questionHolder"> ...some stuff here ... </div>
<div id="questionOrder_3" class="questionHolder"> ...some stuff here ... </div>
但是一旦我拖放了它,如果我将 div #questionOrder3 移动到顶部,就会像这样:
<div id="questionOrder_3" class="questionHolder"> ...some stuff here ... </div>
<div id="questionOrder_0" class="questionHolder"> ...some stuff here ... </div>
<div id="questionOrder_1" class="questionHolder"> ...some stuff here ... </div>
<div id="questionOrder_2" class="questionHolder"> ...some stuff here ... </div>
php 脚本更新的数据库会相应更改,但一旦发生拖放,我需要实时更改 div id。
我希望这是有道理的。
I'm still a little new to jQuery and need a little help.
I am using a drag and drop script as below.
$(document).ready(function() {
$(function() {
$("#contentLeft ul").sortable({
opacity: 0.6,
cursor: 'move',
update: function() {
var order = $(this).sortable("serialize") + '&action=updateRecordsListings';
$.post("updateDB.php", order, function(theResponse) {
$("#contentRight").html(theResponse);
});
}
});
});
});
theResponse contains a dynamic array like below once the drag and drop is complete and the php server side script has parsed.
Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 2
)
What i need to be able to do is once i have the response - rename the div ids of the corresponding divs, so the top one will get questionOrder_0, the next questionOrder1 the next questionOrder2 so on and so on.
The divs on page load are created dynamically like below.
<div id="questionOrder_0" class="questionHolder"> ...some stuff here ... </div>
<div id="questionOrder_1" class="questionHolder"> ...some stuff here ... </div>
<div id="questionOrder_2" class="questionHolder"> ...some stuff here ... </div>
<div id="questionOrder_3" class="questionHolder"> ...some stuff here ... </div>
But once i have dragged and dropped it would like this if i move say div #questionOrder3 to the top :
<div id="questionOrder_3" class="questionHolder"> ...some stuff here ... </div>
<div id="questionOrder_0" class="questionHolder"> ...some stuff here ... </div>
<div id="questionOrder_1" class="questionHolder"> ...some stuff here ... </div>
<div id="questionOrder_2" class="questionHolder"> ...some stuff here ... </div>
The database that the php script updates is altered accordingly but i need to alter the div ids live once a drop has taken place.
I hope this makes sense .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个简单的示例,可以帮助您上手。 http://jsfiddle.net/LcK8G/5/
Here's a simple example that will help you get on your way. http://jsfiddle.net/LcK8G/5/
我不太明白你为什么要操纵 ID。
如果在任何给定时间为任何两个元素分配相同的 ID,则可能会遇到冲突。
最好在服务器端进行一些排序或代替 JS 中的序列化。
I don't exactly understand why you wish to manipulate the ID's.
You can encounter conflicts if any two elements are assigned the same ID at any given time.
It would be best to do some sorting server-side or in place of the serialize in JS.