简体   繁体   English

如何将变量按顺序传递给PHP?

[英]How do I pass variables to PHP on sort?

I am new to Jquery/Ajax - I'm trying to get the Sortable script to pass the values of some to a PHP script. 我是Jquery / Ajax的新手-我正在尝试获取Sortable脚本,以将某些值传递给PHP脚本。

I am trying to use this method as it has some nice UI features: https://sortablejs.github.io/Sortable/ 我正在尝试使用此方法,因为它具有一些不错的UI功能: https : //sortablejs.github.io/Sortable/

Docs: https://github.com/SortableJS/Sortable/blob/master/README.md#toarraystring 文件: https : //github.com/SortableJS/Sortable/blob/master/README.md#toarraystring

I've tried: 我试过了:

var serial = $('#route_list_new').sortable("toArray").val();
var serial = $('#route_list_new').val();
var serial = $("#route_list_new").sortable('serialize');

But i cannot seem to obtain the actual content, i think the error might be somewhere else entirely. 但是我似乎无法获得实际的内容,我认为错误可能完全在其他地方。 (I AM very new to this) (我对此很陌生)

HTML: HTML:


<div id="route_list_new" class="list-group col sortable-list">
  <div class="list-group-item"><p>Number 1</p></div>
  <div class="list-group-item"><p>Number 2</p></div>
  <div class="list-group-item"><p>Number 3</p></div>
</div>

SCRIPT: 脚本:


route_list_new = document.getElementById('route_list_new');
new Sortable(route_list_new, {
    group: 'shared',
    animation: 250,
    onSort: function (event, ui) {
        var serial = $("#route_list_new").sortable('serialize');
        $.ajax({
            data: { serial },
            type: 'POST',
            url: 'sortable.php',
        });
    }
});

I would like the output variable "serial" to = 我想将输出变量“ serial”设置为=

"Number 1, Number 2, Number 3"

You may simply use this.toArray() . 您可以简单地使用this.toArray()

However, this won't give you the text values of your items, but their data-id attributes instead, as per the docs . 但是, 根据docs ,这不会为您提供商品的文本值,而是它们的data-id属性。

So you should change your items from this: 因此,您应该从此更改项目:

<div class="list-group-item"><p>Number 1</p></div>

to this: 对此:

<div class="list-group-item" data-id="number1"><p>Number 1</p></div>

where number1 uniquely identifies your item (doesn't have to be the same as its text). 其中number1唯一标识您的商品(不必与商品文字相同)。

 route_list_new = document.getElementById('route_list_new'); new Sortable(route_list_new, { group: 'shared', animation: 250, onSort: function(event, ui) { var sorted = this.toArray(); console.log(sorted); $.ajax({ data: {sorted}, type: 'POST', url: 'sortable.php', }); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/> <div id="route_list_new" class="list-group col sortable-list"> <div class="list-group-item" data-id="number1"><p>Number 1</p></div> <div class="list-group-item" data-id="number2"><p>Number 2</p></div> <div class="list-group-item" data-id="number3"><p>Number 3</p></div> </div> 

Simply change: 只需更改:

var serial = $("#route_list_new").sortable('serialize');

To: 至:

var serial = getSerial();

Then add the following function: 然后添加以下功能:

function getSerial() {
    let items = $("#route_list_new").find('.list-group-item > p'),
        serial = '';

    $.each(items, function(i, num){
        num = $(num);
        serial += num.html() + ', '
    });

    serial.slice(0,-2);

    return serial
}

If you do a console.log(serial) you will get "Number 1, Number 2, Number 3" 如果您执行console.log(serial) ,则会得到“数字1,数字2,数字3”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM