Это мой HTML:
Код: Выделить всё
[list]
[*]Radio1
[*]Radio2
[*]Radio3
[*]Radio4
[/list]
Код: Выделить всё
const radiotest = document.getElementById('radiotest');
radiotest.querySelectorAll('li').forEach(
li => {
// console.log('adding listener to radio item', li);
li.addEventListener('click', (e) => {
// Prevent the native toggle so we fully control selection
e.preventDefault();
e.stopPropagation();
selectItem(li, e).catch(err => console.error(err));
});
}
);
Код: Выделить всё
async function selectItem(li, event) {
return new Promise((resolve) => {
// In case this function is called from other places, be defensive
console.log('selectItem called for', li);
const input = li.querySelector('input');
if (!input) return resolve();
if (input.type === 'radio') {
console.log('radio item clicked:', input.checked); // Allways true
// do nothing if already selected
if (input.checked){ // This allways seems to be true
console.log('radio item already selected, doing nothing');
return resolve();
}
// The following code is never executed
// clear other radios in this list
const radiotest = document.getElementById('radiotest');
radiotest.querySelectorAll('li').forEach(other => {
console.log('clearing radio item class:', other);
other.classList.remove('active');
});
radiotest.querySelectorAll('input[type="radio"]').forEach(r => {
console.log('clearing radio item checked:', r);
r.checked = false;
});
input.checked = true;
li.classList.add('active');
console.log('radiotest selection:', input.value, input.checked);
}
resolve();
});
}
У меня есть скрипт, настроенный с кодом в jsfiddle
Подробнее здесь: https://stackoverflow.com/questions/798 ... ecked-true
Мобильная версия