简体   繁体   English

如何使用jquery找到最接近的元素

[英]How to find the closest element using jquery

I have searched in SO I found so many examples but mine was little different from all. 我搜索过的,我发现了很多例子,但是我的一切都没什么不同。

1) initially i have a row if the user click save & next button it will say you have 3 fields missing 1)最初我有一行,如果用户点击保存和下一个按钮,它会说你有3个字段丢失

2) if the user click the addmore button and he did not type any value in the text field then he click the save and next button it should still say 3 fields missing 2)如果用户单击addmore按钮并且他没有在文本字段中键入任何值,那么他单击保存和下一个按钮它仍然应该说3个字段丢失

3) if the user type any one of the field in the cloned row then he click the save and next button validation should happen with my code first two points are working 3)如果用户键入克隆行中的任何一个字段,那么他单击保存和下一个按钮验证应该发生我的代码前两点正在工作

but the problem is if the user click some more rows and he type in any one of the cloned field then if he click safe and next button the required_Field class was applying in all other field but it should apply only to that row :( 但问题是,如果用户单击更多行并键入任何一个克隆字段,那么如果他单击安全和下一个按钮, required_Field类应用于所有其他字段但它应该仅适用于该行:(

If we can able to find the closest element of the field where the user type then i can able to add required_Field class to those element only 如果我们能够找到用户类型的字段中最接近的元素,那么我可以将required_Field类添加到仅那些元素

I tried like below 我尝试过如下

        function additionalvalidation() {
          var myRow = $(".check").length;

          //$(".cloned_field").css("border","1px solid green");
          $(".cloned_field").change(function() {
                var myField = $(".cloned_field").val().length;
            if (myField >= 0) {
              $(".cloned_field").addClass("required_Field");
              debugger;
              $(".cloned_field").prevAll('label').find('span.required-star').removeClass('text-error-red');
              //bind_validation();
            } else {
              $(this).closest(".cloned_field").removeClass("errRed");
              $(".cloned_field").removeClass("text-error-red");
            }
            bind_validation();
            return false;
          });
        }

With my current code i am getting error. 使用我当前的代码,我收到错误。

\n

Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“长度”

$(this).closest(".cloned_field").addClass("required_Field");

I tried this also 我也尝试了这个

$(this).closest(".cloned-row1").addClass("required_Field");

Any suggestion for this question 对此问题有任何建议

I tried this new one -> 我尝试了这个新的 - >

$(this).closest(".cloned_field").css({"color": "red", "border": "2px solid red"});

the color red and red border was applying only for that field not for the row :( 颜色红色和红色边框仅适用于该字段不适用于行:(

Fiddle link for the reference 小提琴链接供参考

@mahadevan try this code. @mahadevan试试这段代码。 I working 我在工作

        function additionalvalidation() {
          var myRow = $(".check").length;

          //$(".cloned_field").css("border","1px solid green");
          $(document).on("change",".cloned_field",function() {
          var myField = $(this).val();
          var $clonedDiv  = $(this).closest('.cloned_div');

            if (myField!='') {     
                $clonedDiv.find(".cloned_field").addClass("required_Field");
                $clonedDiv.find(".deg_date").addClass("required_Field");
                $clonedDiv.find(".trans_date").addClass("required_Field");
                $clonedDiv.find(".txt_degreName").addClass("required_Field");


              debugger;
              $(".cloned_field").prevAll('label').find('span.required-star').removeClass('text-error-red');
              //bind_validation();
            } else {
                $clonedDiv.find(".cloned_field").removeClass("errRed");
                $clonedDiv.find(".deg_date").removeClass("errRed");
                $clonedDiv.find(".trans_date").removeClass("errRed");
                $clonedDiv.find(".txt_degreName").removeClass("errRed");

                //$(".cloned_field").removeClass("text-error-red");
            }
            bind_validation();
            return false;
          });
        }

Jquery next maybe the answer for you 接下来Jquery可能就是你的答案

Description: Get the immediately following sibling of each element in the set of matched elements. 描述:获取匹配元素集中每个元素的紧随其后的兄弟。 If a selector is provided, it retrieves the next sibling only if it matches that selector. 如果提供了选择器,则仅当它与该选择器匹配时,它才会检索下一个兄弟。

https://api.jquery.com/next/ https://api.jquery.com/next/

You show know closest search in parents tree and find closest one. 您在父树中显示最近的搜索并找到最近的搜索。 If you want to search in siblings you should execute find on siblings. 如果你想在兄弟姐妹中搜索,你应该在兄弟姐妹上执行find。

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

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