2

我有 2 个复选框。我需要检查任何一个。我知道使用 with Jquery,这很容易。但是 HTML 中的任何默认选项。我正好有 2 个复选框。

谢谢。

代码:

<input type="checkbox" value="1" name="foo" />
<input type="checkbox" value="2" name="foo" />

以及更改复选框高度和宽度的任何选项?像CSS。

4

5 回答 5

2

嗨,在 HTML 中使用单选按钮。... 简单的替代方法.. 更少的代码.. 这也提高了性能.. 优化的解决方案

<form action="">
<input type="radio" name="foo" value="1">
<br>
<input type="radio" name="foo" value="2">
</form>
于 2013-07-22T06:51:37.083 回答
1

如果您可以使用看起来像复选框的单选按钮。尝试这个..

HTML

<input type="radio" value="1" name="foo"  />
<input type="radio" value="2" name="foo"  />

CSS

input[type="radio"] {
    -webkit-appearance: checkbox;
    -moz-appearance: checkbox;
    -ms-appearance: checkbox;     /* not currently supported */
    -o-appearance: checkbox;      /* not currently supported */
}
于 2013-07-22T07:03:56.473 回答
0

如果您想一次选中两个复选框,则删除name语法中的属性。

<input type="checkbox" value="1" />
 <input type="checkbox" value="2"/>
于 2013-07-22T06:49:19.377 回答
0

CSS:

input
{
    width: xpx;
    height: xpx;
}

要检查一项,请使用已检查的属性。

<input type="checkbox" value="1" name="foo" checked />
于 2013-07-22T06:51:38.497 回答
0

这应该足够好

var $inputs = $('input');
$('input').change(function() {
    if(this.checked)
        $inputs.not(this).prop('checked', !this.checked);
});

检查小提琴

无论复选框的数量如何,这都应该有效。

香草JS

var inputs = document.getElementsByTagName('input'),
    checkboxes = [];

for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].type === 'checkbox') {
        checkboxes.push(inputs[i]);
        inputs[i].addEventListener('change', function () {
            if (this.checked) {
                for (var j = 0; j < checkboxes.length; j++) {
                    if (checkboxes[j] !== this) {
                        checkboxes[j].checked = false;
                    }
                }
            }
        });
    }
}

香草 Js 小提琴

于 2013-07-22T06:51:53.327 回答