简体   繁体   English

读取所有下拉列表的选定值

[英]Read selected value of all the drop down list

In my application we have many drop down lists. 在我的应用程序中,我们有许多下拉列表。 And these count of these lists will change depending on user selection. 这些列表的这些计数将根据用户选择而改变。 I need to read the selected value of all the dropdown lists. 我需要读取所有下拉列表的选定值。 But I couldn't get the following code working. 但是我无法使以下代码正常工作。 I am getting empty value for ids2. 我正在获取ids2的空值。

HTML 的HTML

<div>
    <ul class="nav nav-tabs nav-stacked" data-bind="foreach:fileHeaders">
        <li><div>               
                <select data-bind="options:$parent.parentDDL, optionsText: 'text', optionsValue: 'id',value : parentTypeName, optionsCaption: 'Select Type...'"></select>
               <span class="childselect" data-bind="visible:showChildDDL"><select class="selChildDDL" data-bind="options:childDDL,  value: childSelect" ></select></span> 
            </div>
        </li>
    </ul>
</div>

Javascript Java脚本

 self.saveImportClicked = function (temp) {

            var selVal = $('#selChildDDL').val();

            //var ids = $("select.selChildDDL > option:selected").map(function (n, i) {
            //    return $(n).innerHTML();
            //}).join(",");

            var ids2 = $(".selChildDDL > option:selected").map(function (n, i) { return $(i).innerHTML() }).toArray().join(",");

            //var optiontext;
            //var childid2 = $('#selChildDDL option').each(function (index, option) { 
            //     optiontext = option.innerHTML;
            //                });   

 }; 

Use jquery's html() method instead of innerHTML() : 使用jquery的html()方法而不是innerHTML()

var ids2 = $(".selChildDDL > option:selected").map(function (n, i) { 
    return $(i).html();
}).toArray().join(",");

innerHTML is a property of a browser's native DOM element. innerHTML是浏览器的本机DOM元素的属性 html() is a method that is callable on a jquery object (like $(i) in your example code). html()是可在jquery对象上调用的方法(如示例代码中的$(i) )。

.innerHTML is not a jQuery method. .innerHTML不是jQuery方法。 It is a native DOM property. 它是本机DOM属性。 so try the following. 因此,请尝试以下操作。

var ids2 = $(".selChildDDL > option:selected").map(function(n, i){
    return ($(i)[0].innerHTML);
}).toArray().join(",");

console.log(ids2);

$(i)[0].innerHTML or i.innerHTML do exactly the same thing. $(i)[0].innerHTMLi.innerHTML做完全相同的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM