选中复选框时启用 html 单选按钮(基本 JS 或 CSS)

Enable html radio buttom when checkbox selected (basic JS or CSS)

本文关键字:JS 基本 CSS 单选按钮 复选框 启用 html      更新时间:2023-09-26

我有一个 2 个单选按钮和一个复选框,如下面的代码和小提琴所示。

<body>
<input type = "radio"/> Love
<input type = "radio"/> Hate
<input type = "Checkbox"/> Just do it!
</body>

http://jsfiddle.net/jjaleel/3n1ngwy0/

选中复选框后,我希望自动选择单选按钮"爱"。

最简单的Javascript或CSS代码就是我所需要的。

谢谢。

使用 onclick() 事件处理程序自动选择单选按钮。

<body>
    <input type = "radio" name="loh" id="love1"/> Love
    <input type = "radio" name="loh"/> Hate
    <input type = "Checkbox" onclick="document.getElementById('love1').checked = (this.checked === true) ? true : document.getElementById('love1').checked;"/> Just do it!
</body>

我会这样做(这将在正文底部):

document.getElementById("YOURCHECKBOXID").onchange = function() {
    document.getElementsById("input")[0].checked = true;
}

这是一个jsfiddle:http://jsfiddle.net/9mm2swmz/