基于在选择框中选择哪个选项执行脚本? [英] Execute script based on which option is selected in a select box?

查看:117
本文介绍了基于在选择框中选择哪个选项执行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择框,用户根据选项1,选项2和选项3选择要查看的元素。

I've got a select box where the user selects which elements they want to see based on option 1, option 2 and option 3.

m尝试设置是如果选择选项1选择运行一组命令,如果选项2运行另一个,等等。

So what I'm trying to set up is have if option 1 is selected run one set of commands and if option 2 run another and so on.

我想我只需要知道如何设置jQuery选择器。

I guess I just need to know how to set up the jQuery selector.

推荐答案

你没有指定代码是如何触发的,所以这里有两个常见的方法:

You didn't specify how the code was triggered, so here are two common ways:

$('#yourSelect').change(function() {
  if ($(this).val() == 'something') { 
     // do this
  }
  if ($(this).val() == 'somethingelse') { 
     // do that
  }
});

请注意,虽然我为select使用了onchange事件,但另一个元素的onclick代替:

Note that while I used the onchange event for the select, an onclick for another element would look like this instead:

$('#somethingelse').click(function() {
  if ($('#yourselect').val() == 'something') { 
     // do this
  }
  if ($('#yourselect').val() == 'somethingelse') { 
     // do that
  }
});

这篇关于基于在选择框中选择哪个选项执行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆