javascript - 单独禁用按钮
问题描述
如图示例
我想要
如果单击 button1,则 button2 将被禁用
如果单击 button3,则 button4 将被禁用
我之所以使用 class 和 $this 是因为我不想让按钮发生冲突。否则我会使用 ID 标签,所以如果我有多个按钮,我想要一些东西,它们如何在不冲突的情况下工作
$(function() {
$('#container').on('click', '.btn-checkout', function(e) {
$(this).html('proccesing order');
$(this).attr("disabled", true);
$('.button2') = $(this).attr("disabled", true);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST">
<div id="container">
<h3>this area is alone</h3>
<button type="button" class="btn-checkout">button1</button>
<button type="button" class='button2'>button2</button>
<h3>this area is alone</h3>
<button type="button" class="btn-checkout">button3</button>
<button type="button" class="button2">button4</button>
</form>
解决方案
用于.next()
指代下一个元素。
$(function() {
$('#container').on('click', '.btn-checkout', function(e) {
$(this).html('proccesing order');
$(this).attr("disabled", true);
$(this).next("button").attr("disabled", true);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST">
<div id="container">
<h3>this area is alone</h3>
<button type="button" class="btn-checkout">button1</button>
<button type="button" class='button2'>button2</button>
<h3>this area is alone</h3>
<button type="button" class="btn-checkout">button3</button>
<button type="button" class="button2">button4</button>
</form>
推荐阅读
- mysql - 带有 LIKE 字符串的 MySQL 案例语句
- laravel - 未加入状态室的用户数
- spring-boot - 何时生成访问令牌以及如何管理它?
- javascript - 使用 Flask 完成 ajax 后平滑滚动页面
- vue.js - 如何从组件/模板中获取当前的 nuxt 布局?
- ruby-on-rails - 如何在 RSpec 中模拟块参数?
- python - 绘制具有 x 和 y 方向范围的线
- python - 删除combination_combination数组上的重复对
- git - 我需要在每次启动时重新运行 eval(ssh-agent) 和 ssh-add
- flutter - flutter show open with dialog for map applications