Javascript 为页面上的元素指定数组值
我对jQuery还是有点陌生,需要一些帮助 我正在使用一个拖放脚本,如下所示Javascript 为页面上的元素指定数组值,javascript,jquery,drag-and-drop,Javascript,Jquery,Drag And Drop,我对jQuery还是有点陌生,需要一些帮助 我正在使用一个拖放脚本,如下所示 $(document).ready(function() { $(function() { $("#contentLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() { var order = $(th
$(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服务器端脚本已经解析,响应包含一个动态数组,如下所示
Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 2
)
我需要能够做的是,一旦我得到响应——重命名相应div的div id,这样顶部的一个将得到questionOrder\u 0,下一个questionOrder 1,下一个questionOrder 2,依此类推
页面加载上的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>
…这里有些东西。。。
…这里有些东西。。。
…这里有些东西。。。
…这里有些东西。。。
但一旦我拖放了,如果我将say 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 live
我希望这是有意义的。我不太明白您为什么希望操纵ID 如果在任何给定时间为任意两个元素分配相同的ID,则可能会遇到冲突
最好在服务器端进行一些排序,或者代替JS中的序列化。下面是一个简单的示例,它将帮助您继续前进
把这种误解的事情作为评论。没有回答抱歉没有早点回来,这是我整理事情所需要的第一步。谢谢你:)
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')); /=
});