选择选项时更改变量值,不再选择时返回上一个值(Change variable value when option is selected, return previous value when not

编程入门 行业动态 更新时间:2024-10-13 04:26:02
选择选项时更改变量值,不再选择时返回上一个值(Change variable value when option is selected, return previous value when not selected anymore)

我需要根据选择下拉菜单的哪个选项更新变量的值(我在示例中称为“tot”)。 如果我选择选项1或2 tot必须增加10,如果我选择选项3变量“tot”不得增加。 如果我选择选项1,然后我改变主意并选择选项3,则必须恢复Tot的值。

这是选择的html

<select name='hello' id='hello'> <option>Select an option...</option> <option id='one'>One</option> <option id='two'>Two</option> <option id='three'>Three</option> </select>

这是我写的jquery脚本。 我想我不明白.change函数的功能,因为它不能像我预期的那样工作。

var extra = 0; var tot = 0; $(document).ready(function () { $('#hello').change(function(){ if($('#one').is(':selected')) { extra = 10; } else if($('#two').is(':selected')) { extra = 10; } else if($('#three').is(':selected')) { extra = 0; } tot = tot + extra; }); });

I need to update the value of a variable (which I called "tot" in the example) according to what option of a dropdown menu is selected. If I select option 1 or 2 tot must be increased of 10, if I select option 3 variable "tot" must NOT be increased. If i select option 1, and then I change my mind and select option 3 the value of Tot must be restored.

Here's the html of the select

<select name='hello' id='hello'> <option>Select an option...</option> <option id='one'>One</option> <option id='two'>Two</option> <option id='three'>Three</option> </select>

And here's the jquery script I wrote. I guess I didn't understand the functioning of .change function, because it doesn't work as I expected.

var extra = 0; var tot = 0; $(document).ready(function () { $('#hello').change(function(){ if($('#one').is(':selected')) { extra = 10; } else if($('#two').is(':selected')) { extra = 10; } else if($('#three').is(':selected')) { extra = 0; } tot = tot + extra; }); });

最满意答案

或者(如果我已经理解了你正确的话)....

var tot = 120; //some number picked at random var helloed = false; // flag for whether incremented $(document).ready(function(){ $("#hello").change(function(){ var sel = $(this).find(":selected").attr("id") if (sel == "one" || sel =="two" ) { if (!helloed) { tot += 10; helloed = true; } } else { // 'three' selected if (helloed) { tot -= 10; helloed = false; } } alert ("Tot is now " + tot); }); });

如果有更多选项,我会选择开关而不是IF,但这样做可以用于此示例

Or (if I've understood what you're after correctly)....

var tot = 120; //some number picked at random var helloed = false; // flag for whether incremented $(document).ready(function(){ $("#hello").change(function(){ var sel = $(this).find(":selected").attr("id") if (sel == "one" || sel =="two" ) { if (!helloed) { tot += 10; helloed = true; } } else { // 'three' selected if (helloed) { tot -= 10; helloed = false; } } alert ("Tot is now " + tot); }); });

If there were more options I'd go for a switch rather than an IF but that will do for this example

更多推荐

本文发布于:2023-07-15 07:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1111397.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项   变量值   variable   Change   option

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!