gpt4 book ai didi

javascript - 如何获取点击按钮的相关值?

转载 作者:行者123 更新时间:2023-11-28 03:06:22 25 4
gpt4 key购买 nike

当我单击第一个加号按钮并保存时,它显示正确的值;但是当我单击第二个按钮并保存它时,它显示错误的值。

如何获取点击保存按钮的相关值?

$('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>

最佳答案

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>

关于javascript - 如何获取点击按钮的相关值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60582438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com