【问题标题】:Dynamic select options failing in lightbox灯箱中的动态选择选项失败
【发布时间】:2014-07-25 21:34:40
【问题描述】:

我有一个灯箱,用于存放表单。有两个选择,选择英国或美国,每个都有自己的关联列表。

我已经在灯箱之外创建了它背后的逻辑,但它一旦添加就会失败,并且当您选择任一国家/地区时不会切换到相关选项?

Demo Fiddle

这是脚本,我也在使用灯箱羽毛灯。

 <select class="country">
    <option value="US">US</option>
    <option value="UK">UK</option>
</select>

<select class="model">
    <option></option>
</select>

<script>

$(document).ready(function() {
    var selectValues = {
        "UK": {
            "County1": "",
            "County2": ""
        },
        "US": {
            "State1": "",
            "State2": ""
        }
    };

    var $vendor = $('select.country');
    var $model = $('select.model');
    $vendor.change(function() {
        $model.empty().append(function() {
            var output = '';
            $.each(selectValues[$vendor.val()], function(key, value) {
                output += '<option>' + key + '</option>';
            });
            return output;
        });
    }).change();

});

</script>

【问题讨论】:

    标签: javascript jquery html forms lightbox


    【解决方案1】:

    Lightbox 会创建 div 的副本。首次加载对话框时,您必须连接选择框的事件。幸运的是,您可以获得对刚刚打开的对话框的引用。

    $("#openDialogButton").click(function () {
        var dlg = $.featherlight("#fl1",
            {
                otherClose: ".btn", //any .btn will close the dialog
                closeOnEsc: true
            }
        ); //open the featherlight dialog
        //FIND THE SELECT BOXES within the dialog you have just opened
        var $vendor = dlg.$content.find('select.country');
        var $model = dlg.$content.find('select.model');
        $vendor.change(function () { 
            $model.empty().append(function () {
                var output = '';
                 $.each(selectValues[$vendor.val()], function (key, value) {
                     output += '<option>' + key + '</option>';
                 });
                 return output;
             });
         }).change();
    });
    

    当您来保存对话框的结果时,您不能使用 $("select.model")。您必须从 this 对象导航,例如

    $("#dialogOKButton").click(function () {
        var theCommentText = $("#commentText").val(); // null!
        theCommentText = $(
                $(this).parent()
            ).find("#commentText")
            .val(); // have to navigate from this
        alert("you entered this text " 
            + theCommentText + " and clicked OK");
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      • 2013-06-13
      • 1970-01-01
      相关资源
      最近更新 更多