javascript 如何在 jQuery 弹出窗口中显示下拉选择列表并检索选定的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10448792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:55:47  来源:igfitidea点击:

How to show dropdown-select list in jQuery popup and retrieve selected value

javascriptjqueryhtmljquery-ui

提问by nimrod

I would like to show a select-dropdown list in a jQuery Popup / Dialog. Could you please show me how to do this, and how to retrieve the selected value?

我想在 jQuery 弹出窗口/对话框中显示一个选择下拉列表。你能告诉我如何做到这一点,以及如何检索选定的值吗?

Example code below:

示例代码如下:

<div id='selectPopup'>
    <form name='test'>
        <select  id='inptPAN' name='inptPAN'>
            <option value='1'>item 1</option>
            <option value='2'>item 2</option>
            <option value='3'>item 3</option>
            <option value='4'>item 4</option>
            <option value='5'>item 5</option>
            <option value='6'>item 6</option>
        </select>
    </form>
</div>

Please see this jsFiddlefor some code

请参阅此jsFiddle以获取一些代码

采纳答案by Thulasiram

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../../../Scripts/css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet"
        type="text/css" />
    <script src="../../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="../../../Scripts/jquery-ui-1.8.16.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            // this function is triggered as soon as something changes in the form
            $("select[name='inptPAN']").change(function () {
                //console.log('found change');
              selectDialog('Pan','You had selected, Text: '+ $(':selected',this).text()+' And Value : '+$(this).val());
            });

            function selectDialog(title, text) {
                return $('<div></div>').append(text)
            .dialog({
                resizable: true,
                modal: true,
                buttons: {
                    "OK": function () {
                        $(this).dialog("close");
                    }
                }
            });
            }
        });
    </script>
</head>
<body>
    <div id='selectPopup'>
        <form name='test'>
        <select id='inptPAN' name='inptPAN'>
            <option value='1'>item 1</option>
            <option value='2'>item 2</option>
            <option value='3'>item 3</option>
            <option value='4'>item 4</option>
            <option value='5'>item 5</option>
            <option value='6'>item 6</option>
        </select>
        </form>
    </div>
</body>
</html>

For Demo

用于演示