我如何与交互

时间:2021-07-17 20:45:17

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.

如何在按下代码时让代码执行某些操作。例如,当选择“Long Sleeve”时,如何将变量设置为1,并在选择“Short Sleeve”时将变量设置为2,然后再选择“Long Sleeve”将其设置为1。

Also pardon my noobyness at coding

同时也原谅我在编码方面的骄傲

2 个解决方案

#1


1  

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;
 });

#2


0  

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引用...

#1


1  

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;
 });

#2


0  

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引用...