建议重复的答案没有解决
我有4个隐藏的输入,其中值根据用户操作递增。 Var Id获取每个输入值的id,var var为其值。
<input class="howmanyproducts" id="<?php echo $value; ?>" name="<?php echo $value; ?>" type="hidden" value="0">
我将每组值推送到一个将输出的数组:
8(id), 1(val)
9, 2
3, 5
7, 8
我需要根据val对这些值进行排序。所以上面应该返回:
7, 8
3, 5
9, 2
8, 1
以下是我目前的情况,希望这个问题有道理!
$("#proddiv .howmanyproducts").each(function() {
var id = this.id;
var val = $(this).val();
ids.push([id, +val]);
});
ids.sort(function(b, a) { return a[1] - b[1]; });
$("#productorder").val(ids);
$("#productscore").submit();
下面是我如何检索需要排序的表单数据
$prodorder = $_POST['productorder'];
$array2 = array_unique(explode(',', $prodorder));
我目前的代码根本没有改变订单!
答案 0 :(得分:0)
项目,然后表格将提交。我认为这是你的问题,我测试了你的排序功能,这是真的:
var arr = [];
arr.push([1, 3]);
arr.push([2, 2]);
arr.push([4, 8]);
arr.push([0, 9]);
arr.sort(function (b, a) { return a[1] - b[1]; });
console.log(arr);
答案 1 :(得分:0)
我相信你想按照id
值的降序对val
和val
属性的对象数组进行排序,这是正确的吗?如果是这样,那么sort
将满足您的需求:
let A = [{id: 8, val: 1}, {id: 9, val: 2}, {id: 3, val: 5}, {id: 7, val: 8}]
A.sort((a, b) => a.val < b.val)
[{id: 7, val: 8}, {id: 3, val: 5}, {id: 9, val: 2}, {id: 8, val: 1}]