2016-03-16 71 views
1

我有了這個選擇在我的代碼,選擇一個章節中,我要顯示(image here)數量:更新選擇值(HTML)的按鈕

<select name="select" onchange="javascript:sliders[0].goTo(value);"> 
    <?php for($i=1; $i<$row['nbChapitre']+1; $i++){ ?> 
    <option value="<?php echo $i; ?>">Chapitre <?php echo $i ;?></option> 
    <?php } ?> 
</select> 

當我更新它,它的工作原理完美(滑塊移動到問題章節)。 但是,當我使用它有這樣的代碼按鈕(下一個和以前):

<a href="javascript:sliders[0].goToPrev()"> < </a> 
<a href="javascript:sliders[0].goToNext()"> > </a> 

作品背後的滑塊,但選擇不更新,我不知道該怎麼做.. 。

感謝您的幫助。

編輯:嗯,我覺得我已經給你看我的滑塊功能:

var Slider = function() { this.initialize.apply(this, arguments) } 
    Slider.prototype = { 
     initialize: function(slider) { 
     this.ul = slider.children[0] 
     this.li = this.ul.children 
     this.ul.style.width = (this.li[0].clientWidth * this.li.length) + 'px' 
     this.currentIndex = 0 
     }, 
     goTo: function(index) { 
     if (index < 0 || index > this.li.length - 1) 
     return 
     this.ul.style.left = '-' + (100 * index) + '%' 
     this.currentIndex = index 
     }, 
     goToPrev: function() {this.goTo(this.currentIndex - 1)}, 
     goToNext: function() {this.goTo(this.currentIndex + 1)}}; 

我試着像你說的對它們進行編輯,但沒有什麼變化......

回答

1

如果我明白你的問題正確 - 您的功能會更改滑塊的值,但不會更改其顯示的選項。假設您正在使用jQuery,並且您正在更改prev和next函數上選擇菜單的選定索引,則需要在gotonext()和gotoprev()函數中添加更改函數 - 以便更改顯示的值你已經改變了。

$("[name=select]").change(); 

這將允許您的選擇菜單更新。如果你不改變選定的索引 - 你需要這樣做。當你這樣做時,選擇菜單將會改變。

+0

嗯,我認爲我的上一個和下一個功能沒有得到我的當前選擇的價值,因爲當我手動改變我的選擇值(爲例:第3章) Next函數將不起作用,但Previous函數將用於chapter2,然後下一個函數將再次運行。這很奇怪,我不解釋它。 –

1

你需要做的是更新選定的選項。更新的功能,如下面:

sliders[0].goToPrev = function() { 
    $('select[name="select"] option:selected').prev().attr('selected', 'selected'); 
} 

sliders[0].goToNext = function() { 
    $('select[name="select"] option:selected').next().attr('selected', 'selected'); 
} 
+0

我無法像這樣訪問我的選擇,因爲我的函數在一個js文件中:X –