Stack Overflow на русском Asked by lumberspin on January 31, 2021
У меня есть два фильтра по категориям зачислений и расходов.
Я хочу, чтобы при изменении опции у правого фильтра у левого фильтра менялся selected на первую опцию и делался submit формы. И чтобы это работало и в обратном порядке.
Как это сделать?
Мой код:
<form id="form_filter_history" method="POST">
<select name="month_filter_history" onchange="this.form.submit()">
<option disabled>Период</option>
<option value="1">Ноябрь 2020</option>
<option value="2">Октябрь 2020</option>
<option value="3">Сентябрь 2020</option>
</select>
<select name="income_filter_history" id="income_select" onchange="this.form.submit()">
<option value="" disabled>Категории зачислений</option>
<option value="1">Test 1</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
<select name="expense_filter_history" id="expense_select" onchange="this.form.submit()">
<option value="" disabled>Категории расходов</option>
<option value="1">Test 1</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
</form>
let form = document.querySelector('form')
let filterByIncome = form.income_filter_history
let filterByExpense = form.expense_filter_history
filterByIncome.addEventListener('change', () => {
let defaultOption = [...filterByExpense.options].findIndex(option => option.getAttribute('value', '1'))
filterByExpense.selectedIndex = defaultOption
form.submit()
})
filterByExpense.addEventListener('change', () => {
let defaultOption = [...filterByIncome.options].findIndex(option => option.getAttribute('value', '1'))
filterByIncome.selectedIndex = defaultOption
form.submit()
})
<form method="POST">
<select name="month_filter_history">
<option disabled>Период</option>
<option value="1">Ноябрь 2020</option>
<option value="2">Октябрь 2020</option>
<option value="3">Сентябрь 2020</option>
</select>
<select name="income_filter_history" id="income_select">
<option disabled>Категории зачислений</option>
<option value="1">Test 1</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
<select name="expense_filter_history" id="expense_select">
<option disabled>Категории расходов</option>
<option value="1">Test 1</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
</form>
Answered by RavenTheX on January 31, 2021
Я сделал две функции
function change_selected_income_option()
{
document.getElementById('income_select').value="";
let form = document.getElementById('form_filter_history');
form.submit();
}
function change_selected_expense_option()
{
document.getElementById('expense_select').value="";
let form = document.getElementById('form_filter_history');
form.submit();
}
Answered by lumberspin on January 31, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP