我有这样的无线电按钮:
<input class="check" type="radio" id="radio1" name="group"/>
<label for="radio1">vrai</label>
<input class="check" type="radio" id="radio2" name="group"/>
<label for="radio2">faux</label>
<input class="check" type="radio" id="radio3" name="group"/>
<label for="radio3">75</label>
<input class="check" type="radio" id="radio4" name="group"/>
<label for="radio4">18</label>
我有 :checked
的选定类,但奇怪的是,即使使用IE7.js,它也无法在IE8中起作用。
这是我的JS:
$(document).ready(function() {
$('input:checked').addClass("selected");
$('input').click(function () {
$('input:not(:checked)').removeClass("selected");
$(this).addClass("selected");
});
});
不确定是否必须这样做,但我会重写以下内容,看看它是否会更改某些内容:
$('input').not('input:checked').removeClass("selected");
$(this).addClass("selected");
尝试使用change()
代替click()
$(document).ready(function() {
$('input:checked').addClass("selected");
$('input').change(function () { // <--- HERE
$('input:not(:checked)').removeClass("selected");
$(this).addClass("selected");
});
});
hy, 我已经为您制作了纯JavaScript解决方案,它可以使所有浏览器累积。请看看是否对您有用
<script type="text/javascript">
function check(selected)
{
var inputs= document.getElementsByTagName('input');
for(i=0 ; i < inputs.length; ++i)
{
inputs[i].className="";
}
selected.className="selected";
}
</script>
<input class="check" type="radio" id="radio1" name="group" onclick="check(this);"/>
<label for="radio1">vrai</label>
<input class="check" type="radio" id="radio2" name="group" onclick="check(this);"/>
<label for="radio2">faux</label>
<input class="check" type="radio" id="radio3" name="group" onclick="check(this);"/>
<label for="radio3">75</label>
<input class="check" type="radio" id="radio4" name="group" onclick="check(this);"/>
<label for="radio4">18</label>