【问题标题】:HTML/Javascript - Select same option twice by setting selected index onFocusHTML/Javascript - 通过设置选定的索引 onFocus 两次选择相同的选项
【发布时间】:2017-01-26 20:04:44
【问题描述】:

我正在尝试实现一个调用函数的选择,即使选择了两次相同的选项。根据thread 的其中一个答案,我将 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 尝试在 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();

【问题讨论】:

  • 你试过用onclick代替onchange吗?
  • 它会经常触发,我认为..在这种情况下我必须添加一些条件。我不明白为什么我不能在相关链接中调整答案..
  • 您可以重置该值并更新占位符文本以使其看起来具有持久性选择
  • @Chase 你是什么意思'让它看起来有持久的选择'?

标签: javascript html


【解决方案1】:

我测试了我所知道的所有事情,但没有成功。我想知道您链接的线程并用赞成票回答!我的原因是选定的选项不再是一个选项。但我有一个建议:

创建您自己的自定义选择

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

【讨论】:

    【解决方案2】:

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

    因此,与其将选定的索引设置为焦点,不如在处理更改后在 onChange 中进行?这样一来,您的选择将在您处理完当前选择后立即准备好进行另一个选择。

    更新

    好的,看看 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”事件,并且每次重置小部件时,它都会返回初始值。 .

    希望这会有所帮助!

    【讨论】:

    • 您的建议完全有道理,但似乎在 onChange 中将 selectedIndex 设置为 -1 对 onChange 没有影响。查看修改。
    • 嗯——那么 onChange 处理程序中“this”的值是什么?是选择吗?还是 ComboBox 对象?您需要设置 select 的 selectedIndex 值 - 您可能还必须让 ComboBox 知道新的选择。或者可能有一种方法可以直接告诉 ComboBox 重置选择...
    • 我更新了...我想我明白你的意思了。仍然没有运气,但同样,不确定这是否是您的建议
    • 有点像。但是你有一个围绕选择(组合框)的整个类/对象,我怀疑实际的选择被一些自定义控件隐藏了。通常有一种方法可以告诉 ComboBox 之类的插件您要更改所选值...您是否有指向 ComboBox 文档的链接?
    猜你喜欢
    • 2021-03-08
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多