从< select>获取更改的值使用jQuery [英] Get changed value from <select> using jQuery

查看:66
本文介绍了从< select>获取更改的值使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 jQuery 获取< select> 输入的更改后的值.

I am trying to get the changed value of a <select> input using jQuery.

更改前

<select class="input" multiple="multiple" name="inputUserRoles[]">
  <option value="Administrator">Administrator</option>
  <option value="Faculty" selected="selected">Faculty</option>
  <option value="Head of Department">Head of Department</option>
  <option value="Faculty Coordinator" selected="selected">Faculty Coordinator</option>
</select>

更改后

<select class="input" multiple="multiple" name="inputUserRoles[]">
  <option value="Administrator">Administrator</option>
  <option value="Faculty" selected="selected">Faculty</option>
  <option value="Head of Department">Head of Department</option>
  <option value="Faculty Coordinator">Faculty Coordinator</option>
</select>

如果您注意到,在更改事件之后,未选中选项"Faculty Coordinator".

If you notice, after the change event, the option 'Faculty Coordinator' is not selected.

我希望获得教师协调员"的价值.

I wish to get the value 'Faculty Coordinator'.

我的JavaScript代码

$('select[name="inputUserRoles[]"]').change(function(e) {
  // this line gives me the value after the change event.
  var inputUserRoles = $(this).val();
});

可能的解决方案?

我当时认为事件(e)应该包含已更改的数据,但我不知道如何获取它.我正在从事的项目处于最后阶段,我只需要弄清楚这部分即可完成其余类似的模块.

Possible Solutions?

I was thinking that the event (e) should be containing the changed data but I have no idea how to get it. The project I am working on is in its final phase and I just need to figure out this part to complete the similar remaining modules.

完成此操作的另一种方法是获取旧输入并进行比较.

The other way to get this done is by getting the old input and comparing.

推荐答案

做到这一点:

var valueBeforeChange = $('select[name="inputUserRoles[]"]').val();

$('select[name="inputUserRoles[]"]').change(function(e) {
  var inputUserRoles = $(this).val();

  //you can compare here with value before change
  ...

  valueBeforeChange = inputUserRoles;
});

这篇关于从&lt; select&gt;获取更改的值使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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