我比 jQuery 更熟悉 JS,但看起来我需要扩展我对后者的了解才能实现我想要做的事情。
我有这个代码:它有一个在黑色、绿色和红色之间切换的按钮,并且分别不选中框、框 1 或框 2。
JS中的示例:JS 演示
var colors = ["green", "red", "black"];
function setColor(el) {
el.colorIdx = el.colorIdx || 0;
el.style.color = colors[el.colorIdx++ % colors.length];
document.getElementById('box1').checked = el.style.color == 'green';
document.getElementById('box2').checked = el.style.color == 'red';
}
但我需要使脚本更加通用,以便它适用于任何按钮/复选框。这是我到目前为止已经开始的,但我不知道如何将它与像 JS 这样的颜色属性结合起来。
$("input").on('click', function () {
$("[name^=" + this.value + 1 + "]").attr('checked', true)
$("[name^=" + this.value + 2 + "]").attr('checked', true)
})
谢谢你的帮助!