как получить соответствующее значение нажатой кнопки? - PullRequest
1 голос
/ 08 марта 2020

Когда я нажимаю первую кнопку «плюс» и сохраняю ее, она показывает правильное значение; но когда я нажимаю вторую кнопку и сохраняю ее, она показывает неправильное значение.

Как получить соответствующее значение нажатой кнопки сохранения?

$('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);
    });


    $(document).ready(function () {
        $(document).on('click','#save',function () {
            
            let value=$(this).parent().prev().children('#plus_minus_quantity').val();
            let id = $(this).attr("value");
            // console.log(value);
            alert(value);

        });

    })
<html>
<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>
<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>
</div>
<div> <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>
</div>
<div><button type="button" id="save"  value="">SAVE</button></div>
</body>
</html>

1 Ответ

0 голосов
/ 08 марта 2020

let value= $('#plus_minus_quantity').val(); указывает на первое вхождение "#plus_minus_quantity". Вот почему ваш пример всегда предупреждает о значении первого набора. Измените эту строку на let value= $(this).siblings('#plus_minus_quantity').val();, чтобы сослаться на брата нажатой кнопки сохранения.

$('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);
    });


    $(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);

        });

    })
<html>
<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>
<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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...