简体   繁体   中英

What is the correct way to use PARENT and FIND?

I try to get the value of input type=text by finding it in the parent DIV with class "half_weight" . But I get the result NaN and it should be 0.8.

This is HTML:

<div class="add_controller amt no_space half_weight">                   
  <div class="col s2 adds">
    <a id="" href="#" class="button-minus product_quantity_down">
      <i class="material-icons">remove</i>
    </a>
  </div>
  <div class="col s2 adds">
    <a href="#" class="button-plus product_quantity_up">
      <i class="material-icons">add</i>
    </a>
  </div>
  <div class="col s5 qty">
    <div class="row">
      <span class="col s6 qty_value">
        <input type="text" name="" id="" stepquan="0.8" rtype="2" value="0.8" class="text" />  
      </span>          
  </div>
</div>

This is my jQuery:

$('.button-plus').on('click', function() {
    valTemp = parseFloat($(this).parent('.half_weight').find('.qty_value').siblings('input[type=text]').val());
    alert(valTemp);
});

You don't need siblings() (it refers to neighbors and you have got parent-child relationship). I put just

$(this).parents(".half_weight").find('.qty_value input[type=text]').val();

and it shows 0.8.

 $('.button-plus').on('click', function() { var valTemp = parseFloat($(this).parents(".half_weight").find('.qty_value input[type=text]').val()); alert(valTemp); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="add_controller amt no_space half_weight"> <div class="col s2 adds"> <a id="" href="#" class="button-minus product_quantity_down"> <i class="material-icons">remove</i> </a> </div> <div class="col s2 adds"> <a href="#" class="button-plus product_quantity_up"> <i class="material-icons">add</i> </a> </div> <div class="col s5 qty"> <div class="row"> <span class="col s6 qty_value"> <input type="text" name="" id="" stepquan="0.8" rtype="2" value="0.8" class="text" /> </span> </div> </div> 

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