簡體   English   中英

jQuery在循環中向上/向下移動

[英]jquery moving up/down in loop

我試圖上/下移動div中的元素。 一旦移動了item.position必須與前一個交換。 下面是代碼。 有人可以建議如何將新分配的位置和關聯的數據序列傳遞到數組中,以便將它們傳遞給控制器​​並插入數據庫

<c:forEach var="item" items="${entry.value }" >

<ul>
<li> <div class="rows">
          <a id='${item.position}' data-seq='${attr.id}'  class="noDownloadFormat" href="/files/${item.value}" target="_blank"><c:out value="${item.title}" /></a>
         <div style="float:right;margin-top:-15px;">
             <input type="button" class="btn"  value="UP" />

          </div>
      </div>
</li>
<ul>
    </c:forEach>

jQuery的:

 $(document).ready(function () {

   $(".btn1").click(function(){
var $current = $(this).closest('li')
var currentval =$(this).closest('li').children().find("a").prop("id");
var prevVal=$current.prev('li').children().find("a").prop("id");

    var $previous = $current.prev('li');
      if($previous.length !== 0){
        $current.insertBefore($previous);
    $(this).closest('li').children().find("a").prop("id",prevVal);
    $current.prev('li').children().find("a").prop("id")==currentval ;
     }
      return false;
    });



    });

謝謝

我修改了jquery,現在可以上下移動但無法交換位置

我剛按下按鈕... 在線進行調整

<html>
<body>
<ul>
    <li>
        <div class="rows">
            <a id='position1' data-seq='id1'  class="noDownloadFormat" href="#" target="_blank">title1</a>
            <div style="float:right;margin-top:-15px;">
                <input type="button" class="btn" value="UP 1">
            </div>
        </div>
    </li>
    <li>
        <div class="rows">
            <a id='position2' data-seq='id2'  class="noDownloadFormat" href="#" target="_blank">title2</a>
            <div style="float:right;margin-top:-15px;">
                <input type="button" class="btn" value="UP 2">
            </div>
        </div>
    </li>
    <li>
        <div class="rows">
            <a id='position3' data-seq='id3'  class="noDownloadFormat" href="#" target="_blank">title3</a>
            <div style="float:right;margin-top:-15px;">
                <input type="button" class="btn" value="UP 3">
            </div>
        </div>
    </li>
</ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    var handler = function (e) {
        $li = $(e.target).parent().parent().parent();
        // Avoid delete element if it's at top of list.
        if ($li.prev().size() !== 0) {
            // Move element.
            $li.prev().before($li.remove());
            // Bind click handler to new button.
            $('.btn', $($li)).click(handler);
        }
    };
    $('.btn').on('click', handler);
</script>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM