简体   繁体   English

用jQuery更改div ID

[英]change div id on drop with jquery

I have a bit of code that lets me drag and drop elements. 我有一些代码可以让我拖放元素。 The problem is that i don't know how to change the id of the element when it's dropped. 问题是我不知道如何在删除元素时更改其ID。 It needs to stay a list with the right numbers. 它需要保留带有正确数字的列表。

<div class="container">     

    <div class="block">
        <div id="draggables">
            <div id="dragbox1" class="stepbox">
                <label>Step 1</label>
                <input type='text' placeholder="Omschrijving" name="textbox1">
            </div>
            <div id="dragbox2" class="stepbox">
                <label>Step 2</label>
                <input type='text' placeholder="Omschrijving" name="textbox2">
            </div>
            <div id="dragbox3" class="stepbox">
                <label>Step 3</label>
                <input type='text' placeholder="Omschrijving" name="textbox3">
            </div>
        </div>
    </div>

</div>

When i drag a div (for example the div with id dragbox1 down under the div with id dragbox3) i want the jquery to rename the div id's so that it stays in the same order from 1,2,3 and not 2,3,1. 当我拖动一个div时(例如,将id为dragbox1的div向下拖动到id为dragbox3的div之下),我想让jQuery重命名div的id,以使其从1,2,3而不是2,3保持相同的顺序, 1。

I'm thinking it should be sometging like the folowing: 我认为它应该像下面这样:

var divClass = /*div class name*/;
var divId = /*static div id name*/;

var checkDivOrder = function(){
        if(/*check for divs with the name of the divClass*/) {
            if (divId === /*static div id name*/) {
            divId = /*divIdName*/ + 1;
        } else {
            divId = /*divIdName*/ + /*previous div id number*/ + 1;
        }
    }
};

//recall function
if(/*div being dropped*/){
    checkDivOrder(/*prev div number + 1*/);
}

Could someone help me with this as my jquery and javascript skills are not that great. 有人可以帮我这个忙,因为我的jquery和javascript技能不是那么好。

According to my understanding you want you div in order top to down like dragbox1,dragbox2,dragbox3 in sequence. 根据我的理解,您希望div按顺序从上到下依次像dragbox1,dragbox2,dragbox3一样。

you can do like this. 你可以这样

  $('.stepbox').each(function(ind,obj){ $(this).prop('id','dragbox'+(ind+1)); }); 

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

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