简体   繁体   English

从服务器级联下拉菜单加载jQuery对话框

[英]Load jQuery Dialog with cascading dropdowns from server

I have a jQuery dialog that needs to be opened and populated with data from a database. 我有一个jQuery对话框,需要打开并用数据库中的数据填充。 The dialog has in it drop downs that when in a "new" mode (not editing for re-saving) cascade. 该对话框中的下拉菜单是处于“新”模式(不进行编辑以重新保存)时的级联。

How can I load the dialog with the values from the database, while at the same time causing the cascading to happen. 如何在对话框中加载数据库中的值,同时导致级联发生。

I have tied using the onfocus event of the dialog when the dialog is in "edit" mode, but the focus hit every time an element gets focus. 当对话框处于“编辑”模式时,我已使用对话框的onfocus事件进行绑定,但是每次元素获得焦点时都会命中焦点。 Didn't work without being sneaky with the editing mode. 没有偷偷摸摸的编辑模式就无法工作。

I have tried opening the dialog and using jQuery to set the dropdown, which works, but then the cascading does work. 我尝试打开对话框并使用jQuery设置下拉菜单,该菜单有效,但是级联确实可以使用。

For the cascading I am using .change on the the different dropdowns. 对于级联,我在不同的下拉列表中使用.change。

Not sure if the code is going to help, but will post some to itterate the jQuery functionality I am using. 不知道代码是否会有所帮助,但是会发布一些代码来模仿我正在使用的jQuery功能。

The question is: How do I open a dialog, load dropdowns with information from the server and have the .change functionality work? 问题是:如何打开对话框,使用服务器中的信息加载下拉列表,并使用.change功能?

$('#collectDD').change(function(){
      // first change the item drop down list
      var collection = $('#collectDD').val();

      data = "coll=" + collection + "&action=getItems&func=";
      $('#addCollection').text(collection);

      $.ajax({
            url: "getItemList.php",
            type: "GET",
            cache: false,
            data: data,
            success: function (html) {
               $('#itemDD').empty();
               $("#itemDD").html(html);              
               // now update the function collection dropdown
               data = "coll=" + collection + "&action=getFunction";

            }
        });

Collection DD HTML 集合DD HTML

 <select id="collectDD" name="collectionDD">
      <option>Select Collection</option>
      <option>Option1</option>
    </select>

This doesn't exactly match up with your tag names, and I made a little change to the data string, but I think it's in line with what you're looking for 这与您的标签名称不完全匹配,我对data字符串做了一些更改,但是我认为这与您要查找的内容一致

<div id="dialogbox">
    <select id="s1"></select>
    <select id="s2"></select>
    <select id="s3"></select>
</div>

<script type="text/javascript">
  $(document).ready( function() {
    $( "#dialogbox" ).dialog({
        open: function(event, ui) {
            var s1 = $("#s1").empty();
            var s2 = $("#s2").empty(); 
            var s3 = $("#s3").empty();

            s1[0].enabled = false;
            s2[0].enabled = false;
            s3[0].enabled = false;

            //load first dropdown from the database
            var data = "coll=dropdown1&val=&action=getItems&func=";
            $.ajax({
                url: "getItemList.php",
                type: "GET",
                cache: false,
                data: data,
                success: function (html) {
                    s1.html(html);
                    s1[0].enabled = true;
                }
            });

            //load the second DD when the first changes
            s1.change( function() {
                s2[0].enabled = false; //disable until after ajax load
                s3[0].enabled = false;

                data = "coll=dropdown2&val=" + s1.text() + "&action=getItems&func=";
                $.ajax({
                    url: "getItemList.php",
                    type: "GET",
                    cache: false,
                    data: data,
                    success: function (html) {
                        s2.empty().html(html);
                        s2[0].enabled = true;
                    }
                });
            });

            s2.change( function() {
                if (s2[0].enabled) { //test for enabled to prevent some unnessecary loads
                    s3[0].enabled = false;

                    data = "coll=dropdown3&val=" + s2.text() + "&action=getItems&func=";
                    $.ajax({
                        url: "getItemList.php",
                        type: "GET",
                        cache: false,
                        data: data,
                        success: function (html) {
                            s3.empty().html(html);
                            s3[0].enabled = true;
                        }
                    });
                }
            });
        }
    });
  });
</script>

UPDATE 更新

Here is an example with change functions in their own functions 这是一个在自己的功能中具有变更功能的示例

<div id="dialogbox">
    <select id="s1"></select>
    <select id="s2"></select>
    <select id="s3"></select>
</div>

<script type="text/javascript">
    var s1, s2, s3, data;

    $(document).ready( function() {
        s1 = $("#s1").empty();
        s2 = $("#s2").empty(); 
        s3 = $("#s3").empty();

        $( "#dialogbox" ).dialog({
            open: function(event, ui) {
                s1[0].enabled = false;
                s2[0].enabled = false;
                s3[0].enabled = false;

                //load first dropdown from the database
                data = "coll=dropdown1&val=&action=getItems&func=";
                $.ajax({
                    url: "getItemList.php",
                    type: "GET",
                    cache: false,
                    data: data,
                    success: function (html) {
                        s1.html(html);
                        s1[0].enabled = true;
                    }
                });

                //load the second DD when the first changes
                s1.change( changeDD1 );

                //load the third DD when the second changes
                s2.change( changeDD2 );
            }
        });
    });

    function changeDD1() {
        s2[0].enabled = false; //disable until after ajax load
        s3[0].enabled = false;

        data = "coll=dropdown2&val=" + s1.text() + "&action=getItems&func=";
        $.ajax({
            url: "getItemList.php",
            type: "GET",
            cache: false,
            data: data,
            success: function (html) {
                s2.empty().html(html);
                s2[0].enabled = true;
            }
        });
    }

    function changeDD2() {
        if (s2[0].enabled) { //test for enabled to prevent some unnessecary loads
            s3[0].enabled = false;

            data = "coll=dropdown3&val=" + s2.text() + "&action=getItems&func=";
            $.ajax({
                url: "getItemList.php",
                type: "GET",
                cache: false,
                data: data,
                success: function (html) {
                    s3.empty().html(html);
                    s3[0].enabled = true;
                }
            });
        }
    }
</script>

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

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