下拉当前索引 onchange

发布于 2024-08-16 06:49:35 字数 259 浏览 3 评论 0原文

有一个包含 5 个选项的下拉菜单。当前,选择了选项 2。用户现在选择了选项 4。onchange 事件被触发,该事件被 JS 函数捕获,监听选择上的 onchange。

在 JS 函数中,我可以使用 selectedIndex 属性轻松检查用户选择的选项的索引。但是,我还想知道用户更改它的原始值是什么。

是否存在持续存在的属性 基本上是原始值,即 在这种情况下选择 2。

there is a dropdown with 5 options. Currently,option 2 is selected.The user selects option 4 now.The onchange event gets fired which is caught in a JS function listening for onchange on the select.

In the JS function, I can check easily the index of the option selected by the user using the selectedIndex property.However, I want to also know what was the original value that the user changed it from.

Is there a property that persists
basically the original value i.e.
option 2 in this case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Oo萌小芽oO 2024-08-23 06:49:35

作为一个概念,我将以下内容放在一起 - 很可能有更好的方法来做到这一点:

var vals = [];

document.getElementById("colors").onchange = function(){
   vals.unshift(this.selectedIndex);
};

function getPrevious(){
  alert(vals[1]); // you'll need more logic here
                  // this is purposefully simplistic
}

--

<select id="colors">
   <option>Red</option>
   <option>Green</option>
   <option>Blue</option>
</select>

封闭示例:

我试图将这一切与实际的下拉元素本身联系起来。老实说,这是我第一次向下拉菜单本身添加功能,所以我不能保证这不会产生后果:

   var colors = document.getElementById("colors");
       colors.vals = [];
       colors.setPrevious = function() { this.vals.unshift(this.selectedIndex); }
       colors.getPrevious = function() { return this.vals[1]; }
       colors.onchange    = function() { this.setPrevious(); this.getPrevious(); }
       // set initial state
       colors.setPrevious();

Just as a concept I put together the following - there may very well be a better way to do this:

var vals = [];

document.getElementById("colors").onchange = function(){
   vals.unshift(this.selectedIndex);
};

function getPrevious(){
  alert(vals[1]); // you'll need more logic here
                  // this is purposefully simplistic
}

--

<select id="colors">
   <option>Red</option>
   <option>Green</option>
   <option>Blue</option>
</select>

Closed-up Example:

I've tried to tie this all into the actual drop-down element itself. In all honesty, this is the first time I've ever add functions to the dropdown itself, so I cannot promise that this won't have consequences:

   var colors = document.getElementById("colors");
       colors.vals = [];
       colors.setPrevious = function() { this.vals.unshift(this.selectedIndex); }
       colors.getPrevious = function() { return this.vals[1]; }
       colors.onchange    = function() { this.setPrevious(); this.getPrevious(); }
       // set initial state
       colors.setPrevious();
笑饮青盏花 2024-08-23 06:49:35

不,您需要将其保留在 js 变量中。

No, you would need to persist that yourself in a js variable.

跨年 2024-08-23 06:49:35

正如 dl 所说,您可以使用简单的隐藏变量来保存值。我们可以在隐藏变量中设置当前值。

<input type = "hidden" id = "previous" name = "previous"></input>

使用 onfocus="setPrevious(this.id)" 设置当前值

JS

function setPrevious(id) {
	document.getElementById("previous").value = document.getElementById(id).selectedIndex ;
}

使用 Onchange() 检查确认按钮输出并设置“前一个”值

document.getElementById(id).selectedIndex = document.getElementById("previous").value;

As told by dl, you can use simple hidden variable to hold the value.We can set the current value in hidden varaible.

<input type = "hidden" id = "previous" name = "previous"></input>

Use onfocus="setPrevious(this.id)" to set the current value

JS

function setPrevious(id) {
	document.getElementById("previous").value = document.getElementById(id).selectedIndex ;
}

Use Onchange() to check the confirm button output and set the "previous" value

document.getElementById(id).selectedIndex = document.getElementById("previous").value;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文