简体   繁体   中英

Getting value of closest input

I have got this HTML :

<div class="kusy"><input value="{{ $array['summary'] }}" type="text" onchange="$(this).closest('form').submit(); return false;" onkeypress='return event.charCode >= 48 && event.charCode <= 57' class="kusy-input" name="summary"></div>

<div class="plus-minus">
  <input type="hidden" name="_token" value="{{ csrf_token() }}" />
  <input type="hidden" name="id" value="{{ $product->id }}" />
  <input type="hidden" name="action" value="edit-summary" />
  <input class="klik-plus" type="submit" value="+">
  <input class="klik-minus" type="submit" value="-">
</div>

And this Javascript :

$(document).ready(function(){

    $('.klik-plus').click(function(){
        alert($(this).closest('.kusy-input').val());
    });

});

When i click on the plus button, it shows me alert with message "undefined", in the value of the inputbox is number 4.

How can i get that value?

Closest will only look in the ancestors and siblings of the elements, so it can't reach your kusy-input.

What you could do would be to reach the parent element and then find your input

$(document).ready(function(){
    $('.klik-plus').click(function(){
        alert($(this).parents().find('.kusy-input').val());
    });
});

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