【问题标题】:jquery loop through li create json process it in c#jquery循环通过li创建json在c#中处理它
【发布时间】:2013-09-26 18:36:41
【问题描述】:

我有数据库中的项目列表

ITEMS
  I_id int,
  I_name varchar(50),
  I_order int

我用ul li显示了

<ul id='items'>
   <li><span class='txt'>item-1</span> <span class='mOrder'>1</span></li>
   <li><span class='txt'>item-2</span> <span class='mOrder'>2</span></li>
   <li><span class='txt'>item-3</span> <span class='mOrder'>3</span></li>
   <li><span class='txt'>item-4</span> <span class='mOrder'>4</span></li>
   <li><span class='txt'>item-5</span> <span class='mOrder'>5</span></li>
   <li><span class='txt'>item-6</span> <span class='mOrder'>6</span></li>
   <li><span class='txt'>item-7</span> <span class='mOrder'>7</span></li>
   <li><span class='txt'>item-8</span> <span class='mOrder'>8</span></li>
   <li><span class='txt'>item-9</span> <span class='mOrder'>9</span></li>
</ul>
<input type='button' id='btnSave' value=' Save order ' />

通过jquery可排序插件排序

$(".items").sortable({
   $(".items li").each(function () {
       var OrderNum = (parseInt($(this).index()) + 1);
       $(".mOrder", this).html(OrderNum);
   });
});

现在我想保存新订单
我有 saveOrder.ashx 文件来更新数据库中的记录
我在用 ajax 发送数据时遇到问题
我正在尝试如下

$("#btnSave").click(function(){
    var arr=[];
    $(".items li").each(function () {
        arr.push({'m'+$(this).index():$(".txt",this).html()});
        //m0:item1, m1:item2,....
    });
    $.ajax({
         url:'',
         data:arr;//here is problem
    });
});

我正在发送这个,但它在 saveOrder.ashx 中无法访问 我可以尝试任何替代方法

【问题讨论】:

  • 发布saveOrder.ashx的代码

标签: c# jquery ajax json


【解决方案1】:

我可能有 2 个答案:

  1. 将数组包装在 JSON 对象中:

    网址:'', 数据:{'arrayofObjects':arr}

  2. JSON.stringify()

    网址:'', 数据:JSON.stringify(arr)

补充:

  1. JSON.stringify() + JSON 对象包装

    网址:'', 数据:JSON.stringify({arrayofObjects:arr})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-02
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多