gpt4 book ai didi

javascript - 如何在div中找到下拉选择的值

转载 作者:行者123 更新时间:2023-11-30 16:38:41 25 4
gpt4 key购买 nike

查看代码:

<div id="maindiv"> 
for(i=1;i<3;i++)
{
<div id="subdiv">
@html.dropdownlistfor(m=>m.id,new selectlist((viewbag.country) as selectlist,"Value","Text"),new{@class="country"})
@html.dropdownlistfor(m=>m.stateid,new selectlist((viewbag.state) as selectlist,"Value","Text"),new{@class="state"})
<input type="button" value="remove" class="remove">
</div>
}
</div>

按钮点击事件:

$("#maindiv").on("click",".remove",function(e){
alert($(".country")[0].selectedIndex); // This jquery returns only zero
$this.parent('div').remove();
});

我需要国家下拉选择索引

最佳答案

您需要使用 closest()家长喜欢,

$("#maindiv").on("click ", ".remove ", function (e) {
var $parent =$(this).closest('div'); // cache parent
alert($parent.find('.country')[0].selectedIndex);
$parent.remove();
});

片段

$("#maindiv").on("click ", ".remove ", function (e) {
var $parent =$(this).closest('div'); // cache parent
alert($parent.find('.country')[0].selectedIndex);
$parent.remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="maindiv">
<!-- id must be unique, so using class -->
<div class="subdiv">
<select class="country">
<option>Select</option>
<option value="1">India</option>
</select>
<input type="button" value="remove" class="remove">
</div>
<div class="subdiv">
<select class="country">
<option>Select</option>
<option value="2">USA</option>
</select>
<input type="button" value="remove" class="remove">
</div>
<div class="subdiv">
<select class="country">
<option>Select</option>
<option value="3">Australia</option>
</select>
<input type="button" value="remove" class="remove">
</div>
</div>

关于javascript - 如何在div中找到下拉选择的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32323809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com