Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   автопереключение <select> (https://javascript.ru/forum/misc/31594-avtopereklyuchenie-select.html)

Vampir3 13.09.2012 17:39

автопереключение <select>
 
всем привет, вот есть выпадающее меню

<select id="option">
<option value="first">first</option>
<option value="second">second</option>
<option value="third">third</option>
<option value="fourth">fourth</option>
<option value="fifth">fifth</option>
</select>

возможно ли сделать кнопки, которые будут переключаться на следующую страницу (first,second,third и тд..) и наоборот

p.s. - на самом деле пунктов в меню намного больше, что и создаёт неудобство переключать их всё время

burashka 14.09.2012 09:53

видимо как-то так:
функция сдвига
function action(shift)
{
    var select = document.getElementById('my_Select_0');

    for(var i=0, len=select.options.length; i<len; i++)
        if(select.options[i].value == select.value)
        {
            if((i+shift>0)&&(i+shift<len))
            {
               select.value = select.options[i+shift].value;
               break;
            }
        }
}

и соответственно кнопка назад вызывает ее с shift=-1, а вперед - с 1

bes 14.09.2012 11:00

Цитата:

Сообщение от Vampir3
которые будут переключаться на следующую страницу

выгрузка других страниц нужна?

<select id="option">
	<option>first</option>
	<option>second</option>
	<option>third</option>
</select>

<button class="previous">&#8592</button>
<button class="next">&#8594</button>

<script>
window.onload = function () {
	var sel = document.body.children[0];
	document.body.onclick = function (e) {
		e = e || event;
		var target = e.target || e.srcElement;
		if (target.className == 'previous') {
			if (sel.selectedIndex != 0) {
				sel.selectedIndex--;
			}
		} else 
		if (target.className == 'next') {
			if (sel.selectedIndex != sel.children.length - 1) {
				sel.selectedIndex++;
			}
		} 
		
	}
}
</script>


PS: http://javascript.ru/formatting

Vampir3 14.09.2012 14:14

Цитата:

Сообщение от bes (Сообщение 204611)
выгрузка других страниц нужна?

<select id="option">
	<option>first</option>
	<option>second</option>
	<option>third</option>
</select>

<button class="previous">&#8592</button>
<button class="next">&#8594</button>

<script>
window.onload = function () {
	var sel = document.body.children[0];
	document.body.onclick = function (e) {
		e = e || event;
		var target = e.target || e.srcElement;
		if (target.className == 'previous') {
			if (sel.selectedIndex != 0) {
				sel.selectedIndex--;
			}
		} else 
		if (target.className == 'next') {
			if (sel.selectedIndex != sel.children.length - 1) {
				sel.selectedIndex++;
			}
		} 
		
	}
}
</script>


PS: http://javascript.ru/formatting

спасибо, всё отлично работает :dance:


Часовой пояс GMT +3, время: 18:27.