Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在div中查找下拉选择的值_Javascript_Jquery_Asp.net Mvc 4 - Fatal编程技术网

Javascript 如何在div中查找下拉选择的值

Javascript 如何在div中查找下拉选择的值,javascript,jquery,asp.net-mvc-4,Javascript,Jquery,Asp.net Mvc 4,查看代码: <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"}) @h

查看代码:

<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>
我需要从国家/地区下拉列表中选择索引

使用选项:selected以获取selected选项

$('.country option:selected').index();
代码

试试这个:

function Selected(country){
    alert(country);
}

@html.dropdownlistfor(m=>m.id,new selectlist((viewbag.country) as selectlist,"Value","Text"),new{ @onchange="Selected(this.value)"})
你需要像这样使用父对象

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

$maindiv.onclick、.remove、函数e{ var$parent=$this.closest'div';//缓存父级 警报$parent.find'.country'[0]。已选择索引; $parent.remove; }; 选择 印度 选择 美国 选择 澳大利亚
缺少qoutes$maindivNot related,但是ViewBag.country已经是一个选择列表了,那么为什么要从中创建一个新的选择列表呢?这只是一个毫无意义的额外开销-它的just@Html.DropDownListForm=>m.id,SelectListViewBag.country,new{@class=country}编辑显示代码不在循环中。除了生成的html无效以及这不是真实代码之外-将来复制真实代码警报$.country[0]。selectedIndex将返回第一个下拉列表的索引,而不是您单击的div中的下拉列表on@StephenMuecke感谢您提供的信息。在此之后,不要使用SelectList。我不确定这段代码的意义是什么-您绑定到模型的属性,但它将无法发回模型,只有第一个下拉列表将绑定,因此您的代码没有意义,但如果您这样做要做到这一点,至少要添加新的{id=},这样它就删除了id属性。重复的id属性无效html@ManoJohnbritto用文本而不是文本进行检查value@ManoJohnbritto要获取所选选项索引,可以使用index。检查更新的答案
function Selected(country){
    alert(country);
}

@html.dropdownlistfor(m=>m.id,new selectlist((viewbag.country) as selectlist,"Value","Text"),new{ @onchange="Selected(this.value)"})
$("#maindiv").on("click ", ".remove ", function (e) {
    var $parent =$(this).closest('div'); // cache parent
    alert($parent.find('.country')[0].selectedIndex);
    $parent.remove();
});