我想在Javascript中创建一个函数,当有人点击/关闭它时,它改变了单选按钮上的属性(这是为了能够检测到单选按钮被选中)。这里是一个jsFiddle解释我想要做的更多一点。 jsFiddle中的代码唯一的问题在于它在jQuery中,我不了解足够的jQuery将它转换回其纯Javascript对应。 Here是我尝试将jQuery转换为Javascript。我完全可以复制代码并使用它,但之后我不会学习任何东西。当单选按钮从选中状态变为未选中状态时,如何执行某些操作?
我一直试图找出最后4个小时的时间,真的很感激任何帮助。
谢谢, 亚历克斯
InitRadio('name');
function InitRadio(name) {
val = 0;
$.each($(':radio[name="' + name + '"]'), function() {
$(this).val(val++);
$(this).attr('chk', '0');
$(this).on("click", function(event) {
SetRadioButtonChkProperty($(this).val(), name);
document.getElementById('1').innerText = document.getElementById('input1').getAttribute('chk');
document.getElementById('2').innerText = document.getElementById('input2').getAttribute('chk');
document.getElementById('3').innerText = document.getElementById('input3').getAttribute('chk');
});
});
document.getElementById('1').innerText = document.getElementById('input1').getAttribute('chk');
document.getElementById('2').innerText = document.getElementById('input2').getAttribute('chk');
document.getElementById('3').innerText = document.getElementById('input3').getAttribute('chk');
}
function SetRadioButtonChkProperty(val, name) {
$.each($(':radio[name="' + name + '"]'), function() {
if ($(this).val() != val)
$(this).attr('chk', '0');
else {
if ($(this).attr('chk') == '0')
$(this).attr('chk', '1');
}
});
}
p {
display: inline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<input type='radio' class='radio-button' name='name' id="input1">
<p id="1"></p>
<input type='radio' class='radio-button' name='name' id="input2">
<p id="2"></p>
<input type='radio' class='radio-button' name='name' id="input3">
<p id="3"></p>
非常感谢转换,它开始变得更有意义。 –