简体   繁体   English

jQuery按顺序放置AND / OR输入框

[英]jQuery placing AND/OR input boxes in order

Hey all I am trying to find the "sweet spot" so that this will work as I am needing it to. 嗨,我正在尝试寻找“最佳地点”,以便它可以按我需要的方式工作。

Below is my current JS code that I am working with. 以下是我正在使用的当前JS代码。

AND BOX code: AND BOX代码:

$("#ANDbtn").click(function() {
   if (numOR > numAND) {
       //Place AND box AFTER previous OR box
       $("#ANDArea_" + (numOR + 1)).after(html);
       console.log('if: ANDArea_' + numOR + ">" + numAND);
   } else if (numAND > numOR) {
       //Place AND box AFTER previous AND box
       $("#ANDArea_" + (numAND - 1)).after(html);
       console.log('else if: ANDArea_' + numAND + ">" + numOR);
   } else {
       //There is no AND box so lets add the first one
       $("#ANDORArea").after(html);
       console.log('else');
   }

   numAND++;
});

OR BOX code: 或BOX代码:

$("#ORbtn").click(function() {
   if (numAND > numOR) {
       //Place OR box AFTER previous AND box
       $("#ORArea_" + (numAND + 1)).after(html);
       console.log('if: ORArea_' + numAND + ">" + numOR);
   } else if (numOR > numAND) {
       //Place OR box AFTER previous OR box
       $("#ORArea_" + (numOR - 1)).after(html);
       console.log('else if: ORArea_' + numOR + ">" + numAND);
   } else {
       //There is no OR box so lets add the first one
       $("#ANDORArea").after(html);
       console.log('else');
   }

   numOR++;
});

Each time the user pushes the button AND it adds 1 to the variable numAND . 每次用户按下按钮,并将添加1可变numAND。 Likewise, when the user pushes the OR button it adds a 1 to the variable numOR . 同样,当用户按下“ 或”按钮时, 会将1添加变量numOR Both variables start at 0 . 这两个变量都0开始。

When the user pushes the AND button it places the AND box on the screen (which goes to the else in the logic above) and if the user pushes the AND button again it places the AND box AFTER the previous AND box (which goes to the else if in the logic above). 当用户按下按钮它把与盒屏幕(都到在上述逻辑的 )上,如果用户按下按钮再次它把盒子以前和包装盒 (这转到否则,如果在上述逻辑中)。

The same goes for the OR box as well. 或”框也是如此。 When the user pushes the OR button it places the OR box on the screen (which goes to the else in the logic above) and if the user pushes the OR button again it places the OR box AFTER the previous OR box (which goes to the else if in the logic above). 当用户按下按钮,它就把OR箱屏幕(都到在上述逻辑的 )上,如果用户再次按下按钮,它把OR前一个或箱 (都到了否则,如果在上述逻辑中)。

So all that works just fine. 因此,一切正常。 Where I am having the issue is when I hit the AND button an AND box shows up and then hit the OR button - it does not place the OR box BELOW the AND box and vise versa . 当我遇到的问题是,当我击和按钮 AND功能框显示出来,然后点击或按钮 -它放置或盒子下面 AND箱反之亦然

Here's a FIDDLER for you're viewing pleasure. 这是一个供您观看乐趣的过滤器。

You're using the after jQuery function to place some of the elements, which is sticking the elements after the #ANDORArea row instead of at the end of the table. 您正在使用after jQuery函数放置一些元素,这#ANDORArea元素粘贴在#ANDORArea行之后而不是表的末尾。 I believe you want to append the elements to the end of the table. 我相信您希望append元素append到表的末尾。 To do that you'd replace $("#ANDORArea").after(html) with something along the lines of $("#ANDORAreaTableBody").append(html) such that the elements are placed at the end of the table, below the other elements. 为此,您可以将$("#ANDORArea").after(html)替换为$("#ANDORAreaTableBody").append(html) ,以便将元素放置在表格的末尾,在其他元素之下。

Check this fiddle to see it in action. 检查这个小提琴 ,看看它的作用。

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

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