简体   繁体   English

我如何互动<option>

[英]How Can I Interact With <option>

Lets say I have 可以说我有

<select>
  <option value="longSleeve">Long Sleeve</option>
  <option value="shortSleeve">Short Sleeve</option>
</select>

how can I make my code do something when one is pressed. 当我按下代码时,如何使我的代码执行某些操作。 For example how can I make a variable set to 1 when 'Long Sleeve' is chosen and set the variable to 2 when 'Short Sleeve' is chosen and then if 'Long Sleeve' is chosen again set it back to 1. 例如,如何在选择“长袖”时将变量设置为1,如何在选择“短袖”时将变量设置为2,然后如果再次选择“长袖”,则将变量设置回1。

Also pardon my noobyness at coding 也请原谅我的编码

What you are looking for is the onchange event handler, in jQuery you can use .change() or .on('change', handler) like this: 您正在寻找的是onchange事件处理程序,在jQuery您可以使用.change().on('change', handler)如下所示:

var someVariable;
$('select').on('change', function(){
     someVariable = this.selectedIndex + 1;
 });

with jquery you can always add listeners like 使用jquery,您可以随时添加侦听器,例如

.click()

to any tag on your html code, maybe you can add an id to that tag 到您的html代码上的任何标签,也许您可​​以向该标签添加ID

<option id="firstOption" value="longSleeve">Long Sleeve</option>

and have something like 并有类似的东西

$("#firstOption").click(function(){
   alert("click First option!");
});

hope this help : ) 希望这个帮助:)

link to jquery reference... 链接到jQuery参考...

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

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