jQuery on('change'...在多个标签中不起作用

编程入门 行业动态 更新时间:2024-10-22 08:36:02
本文介绍了jQuery on('change'...在多个标签中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我使用jquery .on('change'function()

I can't get the .val() of my second input select when I use jquery .on('change' function()

我有2个选择元素:

<select id="itemsA"> <option selected disabled>Select option</option> <option value="a">Item A</option> <option value="b">Item B</option> <option value="c">Item C</option> <option value="d">Item D</option> </select> <select id="itemsB"> <option selected disabled>Select option</option> <option value="1">Item 1</option> <option value="2">Item 2</option> <option value="3">Item 3</option> <option value="4">Item 4</option> </select>

这是我的jquery代码

Here is my jquery code

$(document).ready(function(){ $("select").on('change', function(){ var mySel = $("select").val(); console.log(mySel); }); });

我得到第一个输入选择的值,但第二个输入总是将null作为值检查.

I get the value of the first input select but the second one always give me null as value check.

推荐答案

$(document).ready(function(){ $("select").on('change', function(){ var mySel = $(this).val(); console.log(mySel); }); });

您需要使用this!

**当您使用$('select')时,val函数只能返回一个字符串,因此在您的情况下,它返回的是2组成的选择中的第一个可用值

** When you were using $('select') the val function can return a string only so it returns the first available value out of the selection made of 2 in your case

更多推荐

jQuery on('change'...在多个标签中不起作用

本文发布于:2023-11-27 04:17:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   中不   标签   jQuery   change

发布评论

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

>www.elefans.com

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