本文介绍了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'...在多个标签中不起作用
发布评论