duandu8202 2012-03-19 09:31 采纳率: 0%
浏览 28
已采纳

jQuery Ajax,在列表之间移动列表项

I'm not sure if returning html is the way to go here but I'm not experienced at all with JSON and maybe that's the answer to my problem.

I have two ul lists with items, they all have a select in them and the first list contains all the items with the selected option equal to cero, the other list has the rest:

<ul id="invlistsinmesa">
  <li id="inv-56">
      <label>Name 1</label>
        <select>
            <option  value="1">Mesa 1</option>
            <option  value="2">Mesa 2</option>
            <option  value="3">Mesa 3</option>
                    <!-- ... -->                               
            <option selected="selected" value="0">Sin mesa</option>
        </select>
       <div class="delete_box"><a href="#invlistsinmesa" class="close">Delete</a></div>                  
 </li><li id="inv-57">
      <label>Name 2</label>
        <select>
            <option  value="1">Mesa 1</option>
            <option  value="2">Mesa 2</option>
            <option  value="3">Mesa 3</option>
                    <!-- ... -->                               
            <option selected="selected" value="0">Sin mesa</option>
        </select>
       <div class="delete_box"><a href="#invlistsinmesa" class="close">Delete</a></div>                  
 </li>
</ul>
<ul id="invlist">
  <li id="inv-36">
      <label>Name 1</label>
        <select>
            <option  value="1">Mesa 1</option>
            <option  value="2">Mesa 2</option>
            <option selected="selected" value="3">Mesa 3</option>
                    <!-- ... -->                               
            <option value="0">Sin mesa</option>
        </select>
       <div class="delete_box"><a href="#invlistsinmesa" class="close">Delete</a></div>                  
 </li><li id="inv-37">
      <label>Name 2</label>
        <select>
            <option selected="selected" value="1">Mesa 1</option>
            <option value="2">Mesa 2</option>
            <option value="3">Mesa 3</option>
                    <!-- ... -->                               
            <option value="0">Sin mesa</option>
        </select>
       <div class="delete_box"><a href="#invlistsinmesa" class="close">Delete</a></div>                  
 </li>
</ul>

As you see there's a lot of code and it all comes from a database. I have a few ajax calls going on here (deleting, adding new items) but the one I have a problem with is when I change the selected option. This is my code now (on change of the select):

EDIT: Added the click function (it's not change because I'm using jqTransform to style the selects but it's the same as using change with a regular select)

$('li[id^="inv-"] div.jqTransformSelectWrapper ul li a').on('click', function(){
  var item=($(this).closest("li[id^='inv-']")).attr('id');
  var id = parseInt(item.replace("inv-", ""));
  var nummesa= ($(this).closest('li')).attr('class');

$.ajax({
  type: "POST",
  url: "ajax-invi.php",
  dataType: "html",
  data: "id="+id,
  success: function(data) {
    if(data>=0){
      if(data!=0){
        noty({"text":"Person has been moved"});
      }else{
        //Here I need to control if the list item needs to 
        //change to the other list or not
      }          
    }else{
      if(data=='-1'){
        noty({"text":"Table is full, person cannot move there","type":"error"});
      }
    }
  }
 }); 
});

In my ajax-invi.php file I query de database to see if the person can move to the new selected option (basically you put people in tables, you can't place a person in a full table and that needs to be controlled) and I return -1 if the table is full or 0 if it isn't, but if I need to move it then I have a problem because I'm not returning the html that I need to move.

  • 写回答

1条回答 默认 最新

  • duanniubeng2265 2012-03-21 10:07
    关注

    I'm making some assumptions about what you want here, but assuming you want to move the LI from the invlistsinmesa to invlist here is the code you want for moving the data. You don't need any html returned from your call because the HTML is already present in your document. Assuming the id in your .ajax call is the LI id, such as inv-56 then here is the code:

    var $item = $('#' + id);
    $('#invlist').append('<li id=' + id + '>' + $item.html() + '</li>');
    $item.remove();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据