简体   繁体   English

使用按钮更新选择值(html)

[英]Update select value (html) with buttons

I've got this Select in my code which select the number of a chapter that i want to display ( image here ) : 我在我的代码中有了这个Select,它选择了我要显示的章节编号( 此处图片 ):

<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>

When i update it, it works perfectly (the slider move to the asked chapter). 当我更新它时,它工作正常(滑块移至所要求的章节)。 But, when i use the buttons (next and previous) which have this code : 但是,当我使用具有以下代码的按钮(下一个和上一个)时:

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

the slider behind works, but the Select doesn't update, and I don't know how to do that... 后面的滑块有效,但是Select不会更新,我也不知道该怎么做...

Thanks for your help. 谢谢你的帮助。

EDIT : Hum, i think i have to show you my slider's function : 编辑:嗡嗡声,我想我必须向您展示我的滑块功能:

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)}};

I try to edit them as you said, but nothing change... 我会按照您说的进行编辑,但没有任何变化...

If I understand your question correctly - your functions change the value of the slider, but not change its displayed option. 如果我正确理解您的问题-您的函数会更改滑块的值,但不会更改其显示的选项。 Assuming you are using jQuery and also that you are changing the selected index of the select menu on your prev and next functions, you need to add a change function to the gotonext() and gotoprev() functions - in order to change the displayed value that you have changed. 假设您使用的是jQuery,并且还正在更改prev和next函数上的select菜单的选定索引,则需要向gotonext()和gotoprev()函数添加一个change函数-以更改显示的值你已经改变了。

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

This will allow your select menu to update. 这将使您的选择菜单得以更新。 If you are not changing the selected index - you will need to do that. 如果您不更改选定的索引,则需要这样做。 and the select menu will change when you do. 并且选择菜单将在您更改时更改。

What you need to do is update the selected option. 您需要做的就是更新所选的选项。 Update your functions like below: 如下更新您的功能:

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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM