如果我选择第二个下拉框,如何获取第一个下拉框值
how to get first dropdown box value if i select second dropdown box
下面是我动态创建两个下拉列表的代码。如果我选择第二个下拉列表,我想获取第一个下拉列表的选定值。
以下是 HTML:
<div class="selct_drp_dwn" id="row1">
<select class="drop_dwn_mult" id="name1" onchange="changeBox2(this);">
<option>Sample1</option>
<option>Sample2</option>
<option>Sample3</option>
<option>Sample4</option>
</select>
<select class="drop_dwn_mult1" id="name1" onchange="changeBox3(this);">
<option>Please select</option>
<option>sam</option>
</select>
<i class="fa fa-minus-square remove" aria-hidden="true"></i>
<i class="fa fa-plus-square" aria-hidden="true" id="btnAdd"></i>
</div>
以下是我尝试过的jQuery:
function changeBox3(val1)
{
var a = val1.id;
alert(a);
var c = '#'+a;
var b = $(c).closest('select .drop_dwn_mult option:selected').val();
alert(b);//I am getting undefined
}
问题出在最接近的方法上。它应该是:
.closest('.selct_drp_dwn')
然后你应该找到选择:
.find('.drop_dwn_mult').not(val1).find('option:selected')
考虑到这一点,您仍然可以将代码缩短为:
function changeBox3 (val1)
{
var a = $(val1); // <- you passed `this` to val1 which is already a reference
// to the element, thus not need to find the id
var b = a
.closest('.selct_drp_dwn')
.find('.drop_dwn_mult')
.not(val1)
.find('option:selected')
.val();
alert(b);
}
另一个问题:Id 必须是唯一的,您有重复的 name1
.
相关文章:
- RxJS油门行为;立即获取第一个值
- 与杜兰达尔合作的第一个JavaScript项目.尝试从第三方 API 获取数据
- 如何通过连接多个异步javascript调用,从对象数组中排序并获取第一个obejct
- 只获取HTML元素的第一个类
- 获取行中的第一个值“;点击“;并传递给控制器
- 获取此行的第一个 TD
- 使用 javascript 获取文本框中输入的第一个值
- 使用 querySelectorAll 获取具有该类名的所有元素,而不仅仅是第一个
- Vue js 获取数据模型的第一个值
- 从对象中的 javascript 数组中获取第一个值
- 如何使用 JQuery 获取表的第一个 td 的值
- 获取列表中的第一个 DOM 元素
- jQuery 如何获取第一个 LI 父级
- Javascript从数组中获取第一个值,然后跳过它
- 如何使用JavaScript从JSON字典中获取第一个值
- jQuery只从循环中获取第一个id值
- Javascript只从foreach循环中获取第一个id.为什么
- 如何在jQuery中默认获取第一个HTML选项的值
- 获取第一个h1标题,并在新选项卡中打开
h1.html - 从json中获取第一个值