簡體   English   中英

傳遞動態選定的下拉列表ID

[英]pass the dynamically selected dropdown's id

我在javascript中有ddl_SelectedIndexchanged函數,它接受一個下拉列表的參數,就像vb.net中的以下代碼一樣

Private Sub ddl_SelectedIndexchanged(drop as dropdownlist)
   'code
End sub

我的javascript代碼與上面相同

function ddl_SelectedIndexchanged(drop)
{
   //code
}

現在我要調用document.ready上的函數但是在我的頁面中我有5個下拉列表,請考慮ddl1,ddl2,ddl3,ddl4,ddl5

如果選擇了ddl1,則會有一個單獨的代碼。 如果選擇了ddl2,它也有一個單獨的代碼。 但如果兩者都不會被選中,那么任何其他人都可以訪問ddl_selectedIndexchanged()函數。 由於“任何其他”意味着動態選擇我不知道如何傳遞(ddl)參數如下

if(ddl1.selectedIndex == 0)
    ddl_selectedIndexChanged(ddl2);
else if(ddl2.selectedIndex == 0)
    ddl_selectedIndexChanged(ddl1);
else
    ddl_selectedIndexChanged(`dynamic selection of ddl3,ddl4,ddl5`);
<select class="ddcommon" name="dd1">
    <!-- Your options -->
</select>

<select class="ddcommon" name="dd2">
    <!-- Your options -->
</select>

<select class="ddcommon" name="dd3">
    <!-- Your options -->
</select>


<script type="text/javascript">
    // Code requires jquery
    $(function(){

        $('.ddcommon').change(function(e) {

            element_name  = $(this).attr('name');
            element_value = $(this).val();

            switch(element_name) { // start switch

                case 'dd1':
                    // your code
                    break;

                case 'dd2':
                    // your code
                    break;

                case 'dd3':
                    // your code
                    break;                                  

                // .... so on cases

                    } // endswitch
            });
    });
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM