jquery - 存储按钮响应
问题描述
我的屏幕上有两个按钮(1 和 2)。我希望能够将单击哪个按钮存储在变量中,并将总分加 5(如果按钮 1)或减 5(如果按钮 2)。如何存储已单击的按钮?
解决方案
var total = 0
$("#btn1").on("click", function() {
total = total + 5;
$("#txt1").val(total);
});
$("#btn2").on("click", function() {
total = total - 5;
$("#txt1").val(total);
})
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#banner-message {
background: #fff;
border-radius: 4px;
padding: 20px;
font-size: 25px;
text-align: center;
transition: all 0.2s;
margin: 0 auto;
width: 300px;
}
button {
background: #0084ff;
border: none;
border-radius: 5px;
padding: 8px 14px;
font-size: 15px;
color: #fff;
}
#banner-message.alt {
background: #0084ff;
color: #fff;
margin-top: 40px;
width: 200px;
}
#banner-message.alt button {
background: #fff;
color: #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="btn1">+5</button>
<button id="btn2">-5</button>
<input type="text" value="0" id="txt1" />
推荐阅读
- java - 将 TextView 动态添加到排行榜屏幕
- laravel - 在创建新用户时删除 laravel 6 中名称的验证
- django - 数组的值不能是 ManyToManyField
- video - AppleScript 启动和循环视频全屏避免菜单栏
- sql - SQL从字符串中删除非字母字符
- javascript - 如何摆脱 D3JS 中的嵌套 selectAll?
- elixir - ecto 变更集为现有添加关联
- python - dask 数组分块如何用于三个或更多维度?
- python - 如何在 python pandas 中找到两组之间的差异?
- java - 具有本地文件依赖项的 Maven 项目构建错误 501