简体   繁体   中英

jQuery($(this).parent() find not working

I can't seem to get jquery find working on a product grid I have. Any idea how I can set it up?

<div id="sold-out" style="margin-top: 10px">
  <a class="grid-view-item__link grid-view-item__image-container" href=
  "/products/moringa-and-coconut-soap"></a>
  <form accept-charset="UTF-8" action="/contact#contact_form" class=
  "contact-form" id="contact_form" method="post" name="contact_form">
    <a class="grid-view-item__link grid-view-item__image-container" href=
    "/products/moringa-and-coconut-soap"><input name="form_type" type="hidden"
    value="contact"><input name="utf8" type="hidden" value="✓"></a>
    <p><a class="grid-view-item__link grid-view-item__image-container" href=
    "/products/moringa-and-coconut-soap"></a><a class=
    "product-page-notify-me notify-me" href="#" style="color: #788188;">Email
    me when available</a></p>
    <div class="clearfix notify-me-wrapper" style="display:none">
      <input class="styled-input" name="contact[email]" placeholder=
      "your@email.com" required="required" style="float:left; width:100%;"
      type="email" value=""> <input name="contact[body]" type="hidden" value=
      "Please notify me when Moringa and Coconut Soap becomes available.">
      <input class="btn styled-submit" style="float:left;" type="submit" value=
      "Send">
    </div>
  </form>
</div>

Javascript code:

jQuery('.notify-me').click(function() {
  alert('hello');
  jQuery($(this).parent().find('.notify-me-wrapper').fadeIn());           
  return false;
});

The code on my other page however, which targets one item, is working fine:

jQuery('#notify-me').click(function() {

jQuery('#notify-me-wrapper').fadeIn();           

return false;
} );

The parent of .notify-me is the p element and this element does not contain any descendant with the calss notify-me-wrapper

You might want to use closest , that way you can search for the closest ancestors for which you are sure that it contains the element with the class notify-me-wrapper :

$(this).closest('.contact-form').find('.notify-me-wrapper').fadeIn()

jQuery.closest() :

For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

The parent of .notify-me is a p tag (paragraph) and the element you are looking for is the next sibling. Hence, change :

jQuery($(this).parent().find('.notify-me-wrapper').fadeIn());    

with:

jQuery($(this).parent().next('.notify-me-wrapper').fadeIn());

Change .find() with .next() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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