【问题标题】:How can I change nearest input with jQuery?如何使用 jQuery 更改最近的输入?
【发布时间】:2021-07-25 16:17:45
【问题描述】:

我正在编辑 woocomerce 插件。我想创建一个数量按钮,点击时将更改最近的产品数量。

<div class='product-1'>
    <div class='changeQtyButton'>
         <button class='buttonNumber'>1</button>
         <button class='buttonNumber'>2</button>
         <button class='buttonNumber'>3</button>
    <div>
    <div class='quantity'>
         <input class='quantityInput-1'>
    <div>
</div>
<div class='product-2'>
    <div class='changeQtyButton'>
         <button class='buttonNumber'>1</button>
         <button class='buttonNumber'>2</button>
         <button class='buttonNumber'>3</button>
    <div>
    <div class='quantity'>
         <input class='quantityInput-2'>
    <div>
</div>

<script>
    jQuery('button').on('click', function(){
       //inputQty = jQuery(this).next('input').attr('name') ;
        test = jQuery(this).closest( "div.changeQtyButton" ).find("input[name=''quantity]").val();
       console.log(test);
    });
 </script>

那么如何在单击按钮时选择最近的输入?

【问题讨论】:

标签: javascript html jquery


【解决方案1】:

你需要上一层,找到下一个元素并在里面输入。

$('button').on('click', function() {
    $(this).parent().next().find('input')
})

.parent().changeQtyButton
.next().quantity

或者

$(this).closest('[class^=product-]').find('input')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2016-05-25
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    相关资源
    最近更新 更多