简体   繁体   English

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

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

I'm trying to implement a select that calls a function even if the same option is selected twice. 我试图实现一个选择,即使两次选择相同的选项,该选择也会调用函数。 Following one of the answers on this thread , I've set the selectedIndex to -1 on focus. 在此线程中的答案之一之后,我将focus的selectedIndex设置为-1。 However, I'm still not getting the function call when the same option is selected twice. 但是,当两次选择相同的选项时,我仍然没有得到函数调用。

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 Attempting to set the selected index within onChange still doesn't call the function--wondering if this has to do with the fact that selected index is undefined onChange.. 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();

So I think the problem is that after you've picked a value in the select, it doesn't lose focus. 因此,我认为问题在于,在选择中选择了一个值之后,它不会失去焦点。 So your onFocus handler won't get called unless the user actually clicks somewhere else on the page. 因此,除非用户实际单击页面上的其他位置,否则不会调用onFocus处理程序。 If she doesn't do that, then the select's value won't fire the onChange if she selects the same value again. 如果她不这样做,那么如果选择的值再次选择相同的值,则不会触发onChange。

So instead of setting the selected index on focus, why not do it in onChange, after you've handled the change? 因此,在处理更改之后,为什么不将选定的索引置于焦点上,为什么不在onChange中进行呢? That way your select will be primed for another selection as soon as you're done treating the current one. 这样,一旦处理完当前选择项,您的选择项将被准备用于其他选择项。

UPDATE UPDATE

Ok, so looking at the Dojo code, this is the best I could come up with: 好的,所以看一下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();

Note that you need to start the whole thing with no value - or else that initial value won't issue an "onChange" event, and every time you reset the widget, it'll go back to the initial value... 请注意,您需要从无值开始整个事情-否则初始值将不会发出“ onChange”事件,并且每次重置窗口小部件时,它将返回到初始值...

Hope this helps! 希望这可以帮助!

I tested every things that i knew with no success. 我测试了我所知道的所有事情,但是都没有成功。 I wondering about the thread that you linked and answers with upvotes! 我想知道您链接的主题并通过投票回答! I the reason is that a selected option is not an option any more. 我的原因是选定的选项不再是选项。 But i have a suggestion: 但我有一个建议:

Create your own custom Select 创建自己的自定义选择

It is only a textbox and a div under that with some links line by line. 它只是一个文本框和下面的一个div,并带有逐行链接。

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

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