选择 jQuery UI 自动完成中的有效标签时启用 jQuery UI 对话框按钮

发布于 2024-12-21 23:33:05 字数 1402 浏览 3 评论 0原文

我有一个 jQuery UI 对话框,其中包含一个填充有各种标签的 jQuery UI 自动完成输入框。对话框本身的按钮窗格中有两个按钮:“选择”和“取消”。但是,我想根据用户当前在输入框的文本字段中的内容来启用和禁用“选择”按钮。换句话说,自动完成功能使用的可用标签列表包含用户可以选择的所有有效选项。因此,如果他们从自动完成中选择这些标签之一,“选择”按钮将被启用,允许他们单击它。此外,如果他们只是输入正确的标签,而不从自动完成的下拉框中实际选择标签之一,我仍然希望启用此“选择”按钮。

起初,我想每次输入字段与可用标签列表发生变化时,我都会交叉引用输入框中当前的文本,以查看是否应该启用“选择”按钮,但到目前为止,它还没有似乎有效。关于我如何实现这一目标有什么建议吗?到目前为止,这是我尝试过的:

$("#dialog").dialog({
    autoOpen: false,
    buttons: {
        Select: function() {
            // Carry out some proceedure knowing they've selected a valid label.
        },
        Cancel: function() {
            // Cancels the selection and closes the dialog box.
        }
    },
    closeOnEscape: false,
    create: function(event, ui) {
        var labels = ["Bird", "Boar", "Dog", "Dragon", "Ox", "Tiger"];
        $("#input").autocomplete({
            delay: 0,
            source: labels
        });
        $("#input").change(function() {
            $(".ui-dialog-buttonpane button:contains('Select')").button("disable");
            labels.each(function() {
                if($("#input").text() == $(this)) {
                    $(".ui-dialog-buttonpane button:contains('Select')").button("enable");
                    return;
                }
            });
        });
    },
    draggable: false,
    modal: true,
    resizable: false,
    title: "Select Dialog",
    width: 460
});

I have a jQuery UI Dialog Box that contains a jQuery UI Autocomplete input box populated with various labels. The dialog box itself has two buttons in the buttonpane: "Select" and "Cancel." However, I want enable and disable the "Select" button depending upon what the user has currently in the text field of the input box. In other words, the list of available tags that the autocomplete makes use of contains all of the valid options the user can select. So, if they select one of these labels from the autocomplete, the "Select" button will be enabled, allowing them to click it. In addition, if they simply type in the proper label without physically selecting one of the labels from the autocomplete's drop-down box, I still want this "Select" button enabled.

At first, I figured I'd just cross-reference the text currently in the input box each time the input field changed with the list of available labels to see if I should enable the "Select" button, but so far, it hasn't seemed to work. Any suggestions as to how I might accomplish this? Here's what I've tried thus far:

$("#dialog").dialog({
    autoOpen: false,
    buttons: {
        Select: function() {
            // Carry out some proceedure knowing they've selected a valid label.
        },
        Cancel: function() {
            // Cancels the selection and closes the dialog box.
        }
    },
    closeOnEscape: false,
    create: function(event, ui) {
        var labels = ["Bird", "Boar", "Dog", "Dragon", "Ox", "Tiger"];
        $("#input").autocomplete({
            delay: 0,
            source: labels
        });
        $("#input").change(function() {
            $(".ui-dialog-buttonpane button:contains('Select')").button("disable");
            labels.each(function() {
                if($("#input").text() == $(this)) {
                    $(".ui-dialog-buttonpane button:contains('Select')").button("enable");
                    return;
                }
            });
        });
    },
    draggable: false,
    modal: true,
    resizable: false,
    title: "Select Dialog",
    width: 460
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南烟 2024-12-28 23:33:05

您很接近,如何使用 $.inArray 切换按钮?此外,您还需要使用 $(this).val()this.value 来计算 input 元素的值。

$("#dialog").dialog({
    autoOpen: false,
    buttons: {
        Select: function() {
            // Carry out some proceedure knowing they've selected a valid label.
        },
        Cancel: function() {
            // Cancels the selection and closes the dialog box.
        }
    },
    closeOnEscape: false,
    create: function(event, ui) {
        var labels = ["Bird", "Boar", "Dog", "Dragon", "Ox", "Tiger"];
        $("#input").autocomplete({
            delay: 0,
            source: labels
        });
        $("#input").change(function() {
            var enabled = $.inArray(this.value, labels) >= 0 ? "enable" : "disable";
            $(".ui-dialog-buttonpane button:contains('Select')").button(enabled);
        });
    },
    draggable: false,
    modal: true,
    resizable: false,
    title: "Select Dialog",
    width: 460
});

示例: http://jsfiddle.net/kcrBB/


根据评论,这里是使用 keyup 而不是更改的示例:

$("#dialog").dialog({
    open: function () {
        $(".ui-dialog-buttonpane button:contains('Select')").button("disable");
    },
    buttons: {
        Select: function() {
            // Carry out some proceedure knowing they've selected a valid label.
        },
        Cancel: function() {
            // Cancels the selection and closes the dialog box.
        }
    },
    closeOnEscape: false,
    create: function(event, ui) {
        var labels = ["Bird", "Boar", "Dog", "Dragon", "Ox", "Tiger"];
        $("#input").autocomplete({
            delay: 0,
            source: labels,
            select: function () {
                $(".ui-dialog-buttonpane button:contains('Select')").button("enable");       
            }
        });

        $("#input").keyup(function() {
            var enabled = $.inArray(this.value, labels) >= 0 ? "enable" : "disable";
            $(".ui-dialog-buttonpane button:contains('Select')").button(enabled);
        });
    },
    draggable: false,
    modal: true,
    resizable: false,
    title: "Select Dialog",
    width: 460
});

示例: http://jsfiddle.net/kcrBB/16/

You were close, how about using $.inArray to toggle the button? Also, you need to use $(this).val() or this.value to figure out the value of an input element.

$("#dialog").dialog({
    autoOpen: false,
    buttons: {
        Select: function() {
            // Carry out some proceedure knowing they've selected a valid label.
        },
        Cancel: function() {
            // Cancels the selection and closes the dialog box.
        }
    },
    closeOnEscape: false,
    create: function(event, ui) {
        var labels = ["Bird", "Boar", "Dog", "Dragon", "Ox", "Tiger"];
        $("#input").autocomplete({
            delay: 0,
            source: labels
        });
        $("#input").change(function() {
            var enabled = $.inArray(this.value, labels) >= 0 ? "enable" : "disable";
            $(".ui-dialog-buttonpane button:contains('Select')").button(enabled);
        });
    },
    draggable: false,
    modal: true,
    resizable: false,
    title: "Select Dialog",
    width: 460
});

Example: http://jsfiddle.net/kcrBB/


Per the comment, here's an example with keyup instead of change:

$("#dialog").dialog({
    open: function () {
        $(".ui-dialog-buttonpane button:contains('Select')").button("disable");
    },
    buttons: {
        Select: function() {
            // Carry out some proceedure knowing they've selected a valid label.
        },
        Cancel: function() {
            // Cancels the selection and closes the dialog box.
        }
    },
    closeOnEscape: false,
    create: function(event, ui) {
        var labels = ["Bird", "Boar", "Dog", "Dragon", "Ox", "Tiger"];
        $("#input").autocomplete({
            delay: 0,
            source: labels,
            select: function () {
                $(".ui-dialog-buttonpane button:contains('Select')").button("enable");       
            }
        });

        $("#input").keyup(function() {
            var enabled = $.inArray(this.value, labels) >= 0 ? "enable" : "disable";
            $(".ui-dialog-buttonpane button:contains('Select')").button(enabled);
        });
    },
    draggable: false,
    modal: true,
    resizable: false,
    title: "Select Dialog",
    width: 460
});

Example: http://jsfiddle.net/kcrBB/16/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文