简体   繁体   English

使用jQuery / JS向上/向下切换DIV元素

[英]Switch DIV elements up/down with jQuery/JS

I'm trying to move div elements of a dynamic form up or down: 我试图向上或向下移动动态表单的div元素:

Here is my form: 这是我的表格:

  <form name="f_form">
        Order name <input type="text" name="ord" id="order" />

        <div id="fields">
<div id="hid1"><a href="#" id="did1">
<img src="d.jpg" /></a><a href="#" id="uid1">
<img src="u.jpg" /></a><input type="text" name="p_name[]" id="names" />
<input type="text" name="quant[]" id="quant" size="3" />
<input type="text" name="pr[]" id="price" size="10" />
<input type="text" name="sum_a" id="sum" size="10" disabled="disabled"/>
        </div>
<input type="button" name="nauj" id="nau" value="Add item"/><br />
    </div>
    </form>

When I click "add item" button JS is called: 当我点击“添加项目”按钮时,JS被调用:

$("#nau").click(function () {
(newdiv = document.createElement('div')).id = "hid"+ (counter + 1) +"";
newdiv.innerHTML = '<a href="#" id="did'+ (counter + 1) +'"><img src="d.jpg" /></a><a href="#" id="uid'+ (counter + 1) +'"><img src="u.jpg" /></a><input type="text" name="p_name[]" id="names" />
<input type="text" name="quant[]" id="quant" size="3" />
<input type="text" name="pr[]" id="price" size="10" />
<input type="text" name="sum_a" id="sum" size="10" disabled="disabled"/><a href="#" id="delete'+ (counter + 1) +'">X</a>';
             document.getElementById('fields').appendChild(newdiv);
             counter++;
});

Each form fields row has two arrow (up and down) images: <a href="#" id="did'+ (counter + 1) +'"><img src="d.jpg" /></a><a href="#" id="uid'+ (counter + 1) +'"><img src="u.jpg" /></a> 每个表单字段行都有两个箭头(向上和向下)图像: <a href="#" id="did'+ (counter + 1) +'"><img src="d.jpg" /></a><a href="#" id="uid'+ (counter + 1) +'"><img src="u.jpg" /></a>

when I click on arrow I need to move all row of form fields up or down (move <DIV id=hid..> ) tried like this but it didint worked.. (move up function) 当我点击箭头时,我需要向上或向下移动所有表格字段行(移动<DIV id=hid..> )尝试这样但它确实有用..(向上移动功能)

$("a[id^='uid']").on('click', function() {
            var numrow = $(this).attr("id");
            numrow = numrow.substr(3);
            var nr = 1;

            sumrow = numrow - nr;
            var eil = 'id=' + numrow;
            numrowas = "#hid"+numrow;
            srowas = "#hid"+sumrow;
                    $(numrowas).before($(srowas));
        });

Thanks for advices. 谢谢你的建议。

First of all you're not using on() correctly - the event handler isn't binded as you would expect. 首先,你没有正确使用on() - 事件处理程序没有像你期望的那样绑定。 The correct syntax would be: 正确的语法是:

// "watch for" elements matching selector "a[id^='did']" created inside #fields
$("#fields").on('click', "a[id^='did']", function(e) {...})

Second of all, you are trying to operate on id attributes to deremine after/before what div to append your current element. 其次,您正在尝试对id属性进行操作,以便在追加当前元素的div之后/之前进行deremine。 That's messy, a cleaner approach is using .next() and .prev() to determine what is the next or previous node in relation to the clicked one. 这很麻烦,更.prev()方法是使用.next().prev()来确定与被点击的节点相关的下一个或上一个节点是什么。

I've created a fiddle as an example: http://jsfiddle.net/SPgax/22/ 我创造了一个小提琴作为例子: http//jsfiddle.net/SPgax/22/

Just as a side note: your code is messy, I didn't act on it because it's beyond scope of your question. 正如旁注:你的代码很乱,我没有采取行动,因为它超出了你的问题的范围。 No offence :) 没有恶意 :)

Same sort of solution as @WTK, using .prev() , but different approach. 与@WTK相同的解决方案,使用.prev() ,但方法不同。 And oh, I couldn't resist and did some cleanup ;) 哦,我无法抗拒并做了一些清理;)

See http://jsfiddle.net/WmbmF/4/ http://jsfiddle.net/WmbmF/4/

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

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