每个使用jquery在这个对象里面

时间:2015-10-01 10:55:30

标签: jquery each

我有两个具有相同类名的表单。我想检查点击的所有必填字段。 HTML:

   <form action="#" id="form_sample_3" class="form-horizontal normalForm1">
            <div class="form-group c_name_container">
                <div class="col-md-12">
                    <div class="input-group">
                        <span class="input-group-addon">
                            <i class="fa fa-envelope"></i>
                        </span>
                        <input type="text" id="c_name"  class="required" placeholder="Type your Name here"/>
                    </div>
                </div>
            </div>
            <div class="form-group c_email_container">
                <div class="col-md-12">
                    <div class="input-group">
                        <span class="input-group-addon">
                            <i class="fa fa-envelope"></i>
                        </span>
                        <input type="email"  id="c_email" class="required" placeholder="Type your Email Address">
                    </div>
                </div>
            </div>
            <div class="form-actions">
                <div class="row">
                    <div class="col-md-offset-3 col-md-9">
                        <button type="button" id="start_chat" class="btn green">Start Chat</button>
                    </div>
                </div>
            </div>
        </form>

现在,

$(document).on("click", ".normalForm button[type='submit']", function () {
  // this will refer clicked form having class `normalForm`
  // Here i want to looping for all .required class.
  $('.required').each(function(){
         //  it works fine. but it looping also others .required class of other form in same page. How can I looping only inside my clicked form? 


       });
});

3 个答案:

答案 0 :(得分:2)

您应该绑定submit的{​​{1}}事件而不是form,而无需点击提交按钮。然后,只需遍历此form中的所有.required元素:

form

答案 1 :(得分:1)

试试这个:

app.UseCookieAuthentication(options =>
        {
            options.LoginPath = "";
        });

答案 2 :(得分:1)

我会用:

$(this).closest('form').find('.required').each(...)

由于this将是点击的按钮,而不是表单本身,this.form仅为HTML5。