ボタンがクリックされた後、jQueryが関連する値を取得する方法

  • まず、以下のコードを見てください。

引用ブートストラップ、jQuery API

<head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" >
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" ></script>
</head>

HTML

<html>
<body>
<div class="col-md-3 py-5">
    <button type="button" class="btn bg-light border rounded-circle" id="minus">
        <i class="fas fa fa-minus"></i>
    </button>
    <input type="text" id="plus_minus_quantity" value="1" class="form-controls w-25 d-inline">
    <button type="button" class="btn bg-light border rounded-circle" id="plus">
        <i class="fas fa fa-plus"></i>
    </button>
    <button type="button" id="save" value="">SAVE</button>
</div>
<div class="col-md-3 py-5">
    <button type="button" class="btn bg-light border rounded-circle" id="minus">
        <i class="fas fa fa-minus"></i>
    </button>
    <input type="text" id="plus_minus_quantity" value="1" class="form-controls w-25 d-inline">
    <button type="button" class="btn bg-light border rounded-circle" id="plus">
        <i class="fas fa fa-plus"></i>
    </button>
    <button type="button" id="save" value="">SAVE</button>
</div>
</body>
</html>

JavaScript

//  实现点击增加/减少按钮之后的变化
$('div button i.fa-plus').parent().click(function(){
    
    
        let last_value = $(this).parent().children("input").val();
        last_value++;
        $(this).parent().children("input").val(last_value);
    });
    $('div button i.fa-minus').parent().click(function(){
    
    
        let last_value = $(this).parent().children("input").val();
        if(last_value <= 1) return;
        last_value--;
        $(this).parent().children("input").val(last_value);
    });

//点击save之后获取相关值
    $(document).ready(function () {
    
    
        $(document).on('click','#save',function () {
    
    
            let value= $('#plus_minus_quantity').val();
            let id = $(this).attr("value");
            // console.log(value);
            alert(value);

        });

    })

実行後の画像:
ここに画像の説明を挿入

  • 具体的な説明:
  • 最初の追加ボタンをクリックして保存ボタンをクリックすると、ウィンドウは選択した正しい値を取得します。
  • ただし、2番目の増加ボタンをクリックすると、保存ボタンをクリックした後にウィンドウを取得できません。選択した正しい値ですが、最初の増加ボタンの後の値がスローされます。

  • 解決:
  • 1.今回は、jQueryの新しい知識である兄弟メソッドを使用します。特定の兄弟の使用法を確認するには、以下のリンクをクリックしてください。
  • 兄弟メソッド
  • 2.上記のコードを少し変更してみましょう。
$(document).ready(function () {
    
    
        $(document).on('click','#save',function () {
    
    
            // Changed this line to reference the sibling of the button that was clicked
            let value= $(this).siblings('#plus_minus_quantity').val();
            let id = $(this).attr("value");
            // console.log(value);
            alert(value);

        });

    })
  • 3.わかりました!

兄弟直訳は兄弟姉妹の意味です。
つまり、選択した要素の周りのすべての要素を取得し、適切な方法を使用して周囲に変化をもたらすことができます。

おすすめ

転載: blog.csdn.net/weixin_43814775/article/details/104736993