2016-03-16 34 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 –