繁体   English   中英

HTML / Javascript-通过将选定的索引设置为onFocus来选择相同的选项两次

[英]HTML/Javascript - Select same option twice by setting selected index onFocus

我试图实现一个选择,即使两次选择相同的选项,该选择也会调用函数。 在此线程中的答案之一之后,我将focus的selectedIndex设置为-1。 但是,当两次选择相同的选项时,我仍然没有得到函数调用。

var scaleSelect = new ComboBox({
      id: "scale_select",
      style: {width: "150px"},
      name: "scale_select",
      placeHolder: "Choisir une échelle",
      store: scaleStore,
      disabled: true,
      onFocus: function() {
       this.selectedIndex = -1;
       console.log(this.selectedIndex); //-1
      },
      onChange: function(value){
        mapScale = value;
        window.myMap.setScale(mapScale);
      }
    }, "scale_select");
    scaleSelect.startup();

UPDATE尝试在onChange内设置选定的索引仍然不会调用该函数-对此是否与选定的索引未定义的onChange有关的事实感到困惑。

var scaleSelect = new ComboBox({
      id: "scale_select",
      style: {width: "150px"},
      name: "scale_select",
      placeHolder: "Choisir une échelle",
      store: scaleStore,
      disabled: true,
      onChange: function(value){
        mapScale = value;
        window.myMap.setScale(mapScale);
        var mySelect = document.getElementById("scale_select");
        console.log(this.selectedIndex) //undefined
        this.selectedIndex = -1;
        mySelect.selectedIndex = -1;
        console.log(this.selectedIndex); //-1
        console.log(mySelect.selectedIndex); //-1
      }
    }, "scale_select");
    scaleSelect.startup();

因此,我认为问题在于,在选择中选择了一个值之后,它不会失去焦点。 因此,除非用户实际单击页面上的其他位置,否则不会调用onFocus处理程序。 如果她不这样做,那么如果选择的值再次选择相同的值,则不会触发onChange。

因此,在处理更改之后,为什么不将选定的索引置于焦点上,为什么不在onChange中进行呢? 这样,一旦处理完当前选择项,您的选择项将被准备用于其他选择项。

UPDATE

好的,所以看一下Dojo代码,这是我能想到的最好的代码:

var scaleSelect = new ComboBox({
  id: "scale_select",
  style: {width: "150px"},
  name: "scale_select",
  placeHolder: "Choisir une échelle",
  store: scaleStore,
  disabled: true,
  onChange: function(value){
    if(value) { // the .reset() call below will fire the onChange handler again, but this time with a null value
        mapScale = value;
        window.myMap.setScale(mapScale);
        console.log("Changed to", value, this);
        this.reset(); // This is the magic here: reset so you are guaranteed to isseu an "onChange" the next time the user picks something
    }
  }
}, "scale_select");
scaleSelect.startup();

请注意,您需要从无值开始整个事情-否则初始值将不会发出“ onChange”事件,并且每次重置窗口小部件时,它将返回到初始值...

希望这可以帮助!

我测试了我所知道的所有事情,但是都没有成功。 我想知道您链接的主题并通过投票回答! 我的原因是选定的选项不再是选项。 但我有一个建议:

创建自己的自定义选择

它只是一个文本框和下面的一个div,并带有逐行链接。

暂无
暂无

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

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