Want to improve this question? Update the question so it's on-topic for Stack Overflow на русском.
Closed 9 месяцев назад.
есть такая функция, Отписаться. Т.е. есть форма где несколько чекбоксов, выбрал чекбоксы и нажал отписаться вышло сообщение. Функцию указал, она работает как нужно. Задача в том, что я добавил еще кнопку Подтверждение, на нее вешаю похожую функцию, например, Subscribe. Вот только по вызову должно передаваться сразу 2 списка - чекнутые чекбоксы (cheked) и пустые чекбоксы (unchecked).
function Unsubscribe() {
const inputs = document.getElementsByTagName('input');
console.log(inputs);
var ids = "";
for(var i = 0; i < inputs.length; i++) {
console.log(inputs[i]);
if (inputs[i].checked) {
ids += inputs[i].id + ',';
}
}
if (ids.length > 0) {
const Email = document.location.href.split('?')[1];
const url='https:/www.site.com/unsubscribe?' + Email + '&id=' + ids.substring(0, ids.length - 1);
document.location.href=url;
}
};
const bt = document.getElementById( 'unsubscribe' );
const ch = document.querySelectorAll( 'input[type="checkbox"]' );
bt.addEventListener( 'click', click );
function click () {
let href = 'https:/www.site.com/unsubscribe?';
let checked = [ ], unchecked = [ ];
ch.forEach( i => i.checked ? checked.push( i.id ) : unchecked.push( i.id ) );
if ( checked.length ) {
href += 'checked=' + checked.join( ',' );
}
if ( unchecked.length ) {
href += '&unchecked=' + unchecked.join( ',' );
}
// ...
console.log( href );
}
<button id='unsubscribe'>Unsubscribe</button>
<input type="checkbox" id='1'>
<input type="checkbox" id='2'>
<input type="checkbox" id='3'>
Продвижение своими сайтами как стратегия роста и независимости