简体   繁体   English

模糊事件不适用于下拉菜单

[英]blur event not working on dropdown

I have this little script that checks to see if a form is valid before enabling the Submit button:我有这个小脚本,可以在启用提交按钮之前检查表单是否有效:

$('#RegisterForm input').on('keyup blur', function () { 
    if ($('#RegisterForm').valid()) 
    {                   
        EnableSubmitButton();
    } 
    else
    {
        DisableSubmitButton();
    }
});

This works perfectly, except for on the dropdowns.这工作完美,除了下拉菜单。 If they are the last item to be completed, doing so does not trigger the EnableSubmitButton() method.如果它们是要完成的最后一项,则这样做不会触发EnableSubmitButton()方法。 And unlike the other inputs, if you un-selected it (or, in the case of a textbox, clear the contents) then the DisableSubmitButton();与其他输入不同,如果您取消选择它(或者,在文本框的情况下,清除内容)然后DisableSubmitButton(); event does not trigger.事件不触发。

However, if you try and submit the form without the dropdown selected, it WILL trigger the Required error inside the $("#RegisterForm").validate() method.但是,如果您尝试在未选择下拉列表的情况下提交表单,它将触发 $("#RegisterForm").validate() 方法中的Required错误。

<select 
    class="form-control input-sm required error" 
    id="CountryId" 
    name="CountryId" 
    aria-required="true" 
    aria-invalid="true">
        <option value="">Country*</option>
        <option value="1">United States</option>
        ....
</select>

Your original selector is matching only elements of type <input> which does not include <select> .您的原始选择器仅匹配<input>类型的元素,其中不包括<select>

You can modify your selector to include both element types:您可以修改选择器以包含两种元素类型:

$('#RegisterForm input, #RegisterForm select')...

I changed the script from this:我从这个改变了脚本:

$('#RegisterForm input').on('keyup blur', function () { 

To this:对此:

$('#RegisterForm input').on('keyup blur change', function () { 

And that still didn't work.这仍然不起作用。 But, this does work:但是,这确实有效:

    // and for some reason, this dropdown isn't getting captured in the method above, so it gets it's own
    $("#CountryId").change(function () {
        if ($('#RegisterForm').valid()) {
            EnableSubmitBUtton();
        } else {
            DisableSubmitButton();
        }
    });

Seems like a stupid hack, but it gets the job done.看起来像一个愚蠢的黑客,但它完成了工作。 Open to other answers.打开其他答案。

下拉列表应该在表单字段内。然后模糊事件将起作用。

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

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