du8589840 2016-10-14 10:08
浏览 48
已采纳

Jquery .parent在DOM中走得太远了

I'm trying to create a dynamic contact form error check script...it all works fine except for when i try to change the class of the html element directly above an input field..here's my code:

<div class="container">
                <div class="row">
                    <div class="col-sm-4">
                        <h4>Address:</h4>
                        <address>
                            <p>
                                Plot 12<br>
                                Ogudu Phase 2 <br>
                                Ogudu Lagos <br>
                                <a href="mailto:marketing@businessplusng.com">marketing@businessplusng.com</a>
                            </p>
                        </address>
                    </div>
                    <form method="post" id="contactform" class="form-minimal">
                        <div class="col-sm-4"> 
                            <div class="form-group">
                                <label for="name">Name</label>
                                <input type="text" class="form-control" name="name" id="name" placeholder="Enter name"  title="Please enter your name (at least 2 characters)"/>
                                <div class="form-line"></div>
                            </div>

                            <div class="form-group">
                                <label for="email">Email</label>
                                <input type="email" class="form-control" name="email" id="email" placeholder="Enter email" title="Please enter a valid email address"/>
                                <div class="form-line"></div>
                            </div>

                            <div class="form-group">
                                <label for="phone">Phone</label>
                                <input name="phone" class="form-control required digits" type="tel" id="phone" size="30" value="" placeholder="Enter email phone" title="Please enter a valid phone number (at least 10 characters)">
                                <div class="form-line"></div>
                            </div>

                        </div>
                        <div class="col-sm-4">
                            <div class="form-group">
                                <label for="comments">Your Brief</label>
                                <textarea name="comment" class="form-control" id="comments" cols="3" rows="5" placeholder="Enter your message…" title="Please enter your message (at least 10 characters)"></textarea>
                                <div class="form-line"></div>
                            </div>


                        </div>                        
                        <div class="col-md-8 col-md-offset-4">
                            <button name="submit" type="submit" class="btn large primary" id="submit"> Submit</button>
                            <div class="result"></div>
                        </div>
                    </form>

and here's my javascript code

var form = $("#contactform");
var submit= $("#submit");
var name =$("#name");
var email =$("#email");
var comments =$("#comments");
form.submit(function(e) {
    e.preventDefault();
if($("input#name").val().length < 10)
    {
        $(this).parent(".form-group").addClass("has-error");
    }
    else
    {
        submit.addClass("disabled").html("Please wait");
        $.post("email.php",form.serialize(),"text")
        .done(function(data){
            submit.removeClass("disabled").html("Mail Sent");
            alert(data);
        })
        .fail(function(data)
        {   submit.removeClass("disabled").html("Mail Failed");
        });
    }
});

so basically when i call the .parent().addClass()...it goes all the way up to the row div above the address column...how can i fix this to go directly above the name input element(i.e the parent form group div)

any help is greatly appreciated

  • 写回答

1条回答 默认 最新

  • dsgni26260 2016-10-14 10:10
    关注

    The $(this) you are referencing in the form submit function is the form $("#contactform") itself, not the $("input#name"). Thus $(this).parent() is referencing to the <div class="row">.

    One way to achieve what you want to do is $(this).find("input#name").parent(".form-group").addClass("has-error");

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题