jquery 更改 &验证

发布于 2024-12-06 05:50:08 字数 2856 浏览 0 评论 0原文

我们有一个 jquery 自动完成输入框:

<input name="suburb" id="suburb-agency" autocomplete="off" class="text" type="text" value="">

它会下拉,当他们选择一个选项时,另一个选择框会根据所选内容进行更新:

<select class="text" name="state" id="state-agency">
    <option value="notselected">Please select your state...</option>
    <option value="Apple">Apple</option>
    <option value="Pear">Pear</option>
</select>

例如,他们在 #suburb-agency 自动完成中选择 Pear ,然后在 #state-agency 中自动选择 Pear,

效果非常好。

我遇到的唯一问题是 jquery 验证,当选择 Pear 并更改 #state-agency 框时,验证不会显示,尽管如果我从下拉列表中手动选择一个选项,则会显示验证下框。

这是我的 jquery:

/* STATE VALIDATION */
    $("#state-agency").change(function () {
        var state = $('#state-agency').val();
        if (state == "notselected"){
            $('#state-agency').css({"border-color":"#3399ff"});
            $('#state-err').html("You must choose which state you work in.").removeClass("success_msg").addClass("error_msg");
            return false;
        }else{
            $('#state-agency').css({"border-color":"#1f6d21"});
            $('#state-err').html("Thanks for choosing your state!").removeClass("error_msg").addClass("success_msg");
        }
    });

如何修改它,以便在从自动完成中选择它时显示成功消息?而不仅仅是手动?我猜我需要从更改函数进行更改?

编辑(我们的自动完成代码根据要求更新下拉框)

$(function() {
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split(term).pop();
    }

    $( "#suburb-agency" )
        // don't navigate away from the field on tab when selecting an item
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            source: function( request, response ) {
                $.getJSON( "http://www.site.com/folder/script.php", {
                    term    :   extractLast( request.term )
                }, response );
            },
            search: function() {
                // custom minLength
                var term = extractLast( this.value );
                if ( term.length < 2 ) {
                    return false;
                }
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function( event, ui ) {
              if (ui.item) {
                $('#state-agency').val(ui.item.state);
              }
              else {
                $('#state-agency').val('');
              }
            }
        });
});

We have a jquery autocomplete input box:

<input name="suburb" id="suburb-agency" autocomplete="off" class="text" type="text" value="">

This drops down, and when they choose an option, another select box is updated depending on what was chosen:

<select class="text" name="state" id="state-agency">
    <option value="notselected">Please select your state...</option>
    <option value="Apple">Apple</option>
    <option value="Pear">Pear</option>
</select>

Say for example they choose Pear in the #suburb-agency autocomplete, Pear is then selected automatically in #state-agency

That works perfectly.

The only problem I am having is jquery validation, when Pear is selected and the #state-agency box is changed, the validation doesn't show, although it does if I manually select an option from the drop down box.

Here is my jquery:

/* STATE VALIDATION */
    $("#state-agency").change(function () {
        var state = $('#state-agency').val();
        if (state == "notselected"){
            $('#state-agency').css({"border-color":"#3399ff"});
            $('#state-err').html("You must choose which state you work in.").removeClass("success_msg").addClass("error_msg");
            return false;
        }else{
            $('#state-agency').css({"border-color":"#1f6d21"});
            $('#state-err').html("Thanks for choosing your state!").removeClass("error_msg").addClass("success_msg");
        }
    });

How can I modify this so it shows the success message if it is selected from the autocomplete? and not just manually? I'm guessing I need to change from a change function?

Edit (our autocomplete code which updates the dropdown box as requested)

$(function() {
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split(term).pop();
    }

    $( "#suburb-agency" )
        // don't navigate away from the field on tab when selecting an item
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            source: function( request, response ) {
                $.getJSON( "http://www.site.com/folder/script.php", {
                    term    :   extractLast( request.term )
                }, response );
            },
            search: function() {
                // custom minLength
                var term = extractLast( this.value );
                if ( term.length < 2 ) {
                    return false;
                }
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function( event, ui ) {
              if (ui.item) {
                $('#state-agency').val(ui.item.state);
              }
              else {
                $('#state-agency').val('');
              }
            }
        });
});

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

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

发布评论

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

评论(2

左秋 2024-12-13 05:50:08

如果您使用 jQuery 验证,要在更新输入元素后执行验证,您需要调用

 $(selector).valid()

If you are using jQuery validation, to perform validation of input element after you updated it, you need to call

 $(selector).valid()
生死何惧 2024-12-13 05:50:08

我找到了解决这个问题的方法,我需要在自动完成 JS 中的相关事件 ID 上使用 trigger()

I've found a fix for this, I needed to use trigger() on the relevent IDs in the autocomplete JS.

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