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

Mah*_*van 10 html javascript css jquery jquery-validate

我搜索过的,我发现了很多例子,但是我的一切都没什么不同.

1)最初我有一行,如果用户点击保存和下一个按钮,它会说你有3个字段丢失

2)如果用户单击addmore按钮并且他没有在文本字段中键入任何值,那么他单击保存和下一个按钮它仍然应该说3个字段丢失

3)如果用户键入克隆行中的任何一个字段,那么他单击保存和下一个按钮验证应该发生我的代码前两点正在工作

但问题是如果用户点击了更多行并且他键入了任何一个克隆字段,那么如果他点击安全和下一个按钮,required_Field该类正在应用于所有其他字段,但它应该仅适用于该行:(

如果我们能够找到用户类型的字段中最接近的元素,那么我只能将required_Field类添加到这些元素

我尝试过如下

        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;
          });
        }
Run Code Online (Sandbox Code Playgroud)

使用我当前的代码,我收到错误.

未捕获的TypeError:无法读取未定义的属性"长度"

$(this).closest(".cloned_field").addClass("required_Field");
Run Code Online (Sandbox Code Playgroud)

我也尝试了这个

$(this).closest(".cloned-row1").addClass("required_Field");
Run Code Online (Sandbox Code Playgroud)

对此问题有任何建议

我尝试了这个新的 - >

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

颜色红色和红色边框仅适用于该字段不适用于行:(

小提琴链接供参考

Vel*_*Vel 3

@mahadevan 尝试这个代码。我在工作

        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;
          });
        }
Run Code Online (Sandbox Code Playgroud)