javascript - this find label in a form does not work -
i have 2 forms of same class in page. these forms same same elements. on submitting 1 of them, try access elements inside. here forms like:
<form role="form" class="login-form" id="login-form"> {% csrf_token %} <div class="form-group"> <div class="input-icon"> <i class="icon fa fa-user"></i> <input type="text" class="form-control" name="email" placeholder="username" id="email"> </div> </div> <div class="form-group"> <div class="input-icon"> <i class="icon fa fa-unlock-alt"></i> <input type="password" class="form-control" placeholder="password" name="password" id="password"> </div> </div> <div> <!--input type="checkbox" id="remember" name="rememberme" value="forever" style="float: left;"--> <label style="display: none; color: red;" id="incorrect_info">veuillez vérifier vos informations</label> </div> <button class="btn btn-common log-btn">se connecter</button> </form>
now javascript try access elements:
$('.login-form').submit(function(e){ e.preventdefault(); // information email = $(this).find('input[id="email"]').val(); pass = $(this).find('input[id="password"]').val(); $(this).find('label[id="incorrect_info"]').css('display','block'); });
this code works inputs, not label, not found.
edit :
sorry here how code looks like. find called in ajax done.
$('.login-form').submit(function(e){ e.preventdefault(); // information email = $(this).find('input[id="email"]').val(); pass = $(this).find('input[id="password"]').val(); $.ajax({ ....... }).done(function (data) { if (data.success) { window.location.href = data.url; } else { $(this).find('label[id="incorrect_info"]').css('display','block'); } }); });
i figured out. problem $(this) scope different inside ajax function. i've done save before getting it, , reused after:
$('.login-form').submit(function(e){ e.preventdefault(); // information this_form = $(this); email = $(this).find('input[id="email"]').val(); pass = $(this).find('input[id="password"]').val(); $.ajax({ ...... success : function(data, status){ } }).done(function (data) { if (data.success) { window.location.href = data.url; } else { this_form.find('label[id="incorrect_info"]').css('display','block'); } }); });
Comments
Post a Comment