将数组值分配给页面上的元素

发布于 2024-12-04 20:37:27 字数 1766 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ら栖息 2024-12-11 20:37:27

这是一个简单的示例,可以帮助您上手。 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')); /=
});

Here's a simple example that will help you get on your way. 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')); /=
});
羞稚 2024-12-11 20:37:27

我不太明白你为什么要操纵 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文