[英]assign array values to elements on page
我对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。
我希望这是有道理的 。
这是一个简单的示例,可以帮助您顺利进行。 http://jsfiddle.net/LcK8G/5/
var a = ['a','b','c','d'];
//loop through each div, change selector for your needs
$('div').each(function(i,el){
$el = $(el); //grab this iterations element and make a jQuery object out of it
$el.attr('id',a[i]); //change the elements id based on this iterations index
//this just displays the id for you to see how it is working, would be removed later
$el.text($el.attr('id')); /=
});
我不完全理解您为什么要操纵ID。
如果在任何给定时间为任何两个元素分配了相同的ID,您可能会遇到冲突。
最好在服务器端进行一些排序,或者代替JS中的序列化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.